diff --git a/src/deprecated.ts b/src/deprecated.ts new file mode 100644 index 000000000..1eb9cc550 --- /dev/null +++ b/src/deprecated.ts @@ -0,0 +1,45 @@ +import * as fs from 'fs-extra'; + +import { paths } from './constants'; + +/* + This was originally needed because the default + tsconfig.compilerOptions.rootDir was set to './' instead of './src'. + Now that it's set to './src', this is now deprecated. + To ensure a stable upgrade path for users, leave the warning in for + 6 months - 1 year, then change it to an error in a breaking bump and leave + that in for some time too. +*/ +export async function moveTypes() { + const appDistSrc = paths.appDist + '/src'; + + const pathExists = await fs.pathExists(appDistSrc); + if (!pathExists) return; + + // see note above about deprecation window + console.warn( + '[tsdx]: Your rootDir is currently set to "./". Please change your ' + + 'rootDir to "./src".\n' + + 'TSDX has deprecated setting tsconfig.compilerOptions.rootDir to ' + + '"./" as it caused buggy output for declarationMaps and occassionally ' + + 'for type declarations themselves.' + ); + + try { + // Move the typescript types to the base of the ./dist folder + await fs.copy(appDistSrc, paths.appDist, { + overwrite: true, + }); + } catch (err) { + // ignore errors about the destination dir already existing or files not + // existing as those always occur for some reason, re-throw any other + // unexpected failures + // NOTE: these errors mean that sometimes files don't get moved properly, + // meaning that it's buggy / unreliable (see console.warn above) + if (err.code !== 'EEXIST' && err.code !== 'ENOENT') { + throw err; + } + } + + await fs.remove(appDistSrc); +} diff --git a/src/index.ts b/src/index.ts index c8e30fce4..58d8d6cf4 100755 --- a/src/index.ts +++ b/src/index.ts @@ -12,13 +12,11 @@ import { } from 'rollup'; import asyncro from 'asyncro'; import chalk from 'chalk'; -import util from 'util'; import * as fs from 'fs-extra'; import jest from 'jest'; import { CLIEngine } from 'eslint'; import logError from './logError'; import path from 'path'; -import rimraf from 'rimraf'; import execa from 'execa'; import shell from 'shelljs'; import ora from 'ora'; @@ -48,6 +46,7 @@ import { import { createProgressEstimator } from './createProgressEstimator'; import { templates } from './templates'; import { composePackageJson } from './templates/utils'; +import * as deprecated from './deprecated'; const pkg = require('../package.json'); const prog = sade('tsdx'); @@ -101,16 +100,6 @@ async function getInputs( return concatAllArray(inputs); } -async function moveTypes() { - try { - // Move the typescript types to the base of the ./dist folder - await fs.copy(paths.appDist + '/src', paths.appDist, { - overwrite: true, - }); - await fs.remove(paths.appDist + '/src'); - } catch (e) {} -} - prog .version(pkg.version) .command('create ') @@ -140,16 +129,11 @@ prog // Helper fn to prompt the user for a different // folder name if one already exists async function getProjectPath(projectPath: string): Promise { - let exists = true; - try { - // will throw an exception if it does not exists - await util.promisify(fs.access)(projectPath); - } catch { - exists = false; - } + const exists = await fs.pathExists(projectPath); if (!exists) { return projectPath; } + bootSpinner.fail(`Failed to create ${chalk.bold.red(pkg)}`); const prompt = new Input({ message: `A folder named ${chalk.bold.red( @@ -158,6 +142,7 @@ prog initial: pkg + '-1', result: (v: string) => v.trim(), }); + pkg = await prompt.run(); projectPath = (await fs.realpath(process.cwd())) + '/' + pkg; bootSpinner.start(`Creating ${chalk.bold.green(pkg)}...`); @@ -373,7 +358,7 @@ prog `); try { - await moveTypes(); + await deprecated.moveTypes(); if (firstTime && opts.onFirstSuccess) { firstTime = false; @@ -424,7 +409,7 @@ prog async (inputOptions: RollupOptions & { output: OutputOptions }) => { let bundle = await rollup(inputOptions); await bundle.write(inputOptions.output); - await moveTypes(); + await deprecated.moveTypes(); } ) .catch((e: any) => { @@ -453,14 +438,7 @@ async function normalizeOpts(opts: WatchOpts): Promise { } async function cleanDistFolder() { - try { - await util.promisify(fs.access)(paths.appDist); - return util.promisify(rimraf)(paths.appDist); - } catch { - // if an exception is throw, the files does not exists or it is not visible - // either way, we just return - return; - } + await fs.remove(paths.appDist); } function writeCjsEntryFile(name: string) { diff --git a/templates/basic/tsconfig.json b/templates/basic/tsconfig.json index 0fbd5f582..a63b7e132 100644 --- a/templates/basic/tsconfig.json +++ b/templates/basic/tsconfig.json @@ -6,7 +6,7 @@ "importHelpers": true, "declaration": true, "sourceMap": true, - "rootDir": "./", + "rootDir": "./src", "strict": true, "noImplicitAny": true, "strictNullChecks": true, diff --git a/templates/react-with-storybook/tsconfig.json b/templates/react-with-storybook/tsconfig.json index 3eb378a0d..e0b677e59 100644 --- a/templates/react-with-storybook/tsconfig.json +++ b/templates/react-with-storybook/tsconfig.json @@ -6,7 +6,7 @@ "importHelpers": true, "declaration": true, "sourceMap": true, - "rootDir": "./", + "rootDir": "./src", "strict": true, "noImplicitAny": true, "strictNullChecks": true, diff --git a/templates/react/tsconfig.json b/templates/react/tsconfig.json index 0fbd5f582..a63b7e132 100644 --- a/templates/react/tsconfig.json +++ b/templates/react/tsconfig.json @@ -6,7 +6,7 @@ "importHelpers": true, "declaration": true, "sourceMap": true, - "rootDir": "./", + "rootDir": "./src", "strict": true, "noImplicitAny": true, "strictNullChecks": true, diff --git a/test/fixtures/build-default/tsconfig.json b/test/fixtures/build-default/tsconfig.json index d8ff04c12..7f2bd50c5 100644 --- a/test/fixtures/build-default/tsconfig.json +++ b/test/fixtures/build-default/tsconfig.json @@ -4,7 +4,7 @@ "lib": ["dom", "esnext"], "declaration": true, "sourceMap": true, - "rootDir": "./", + "rootDir": "./src", "strict": true, "noImplicitAny": true, "strictNullChecks": true, diff --git a/test/fixtures/build-invalid/tsconfig.json b/test/fixtures/build-invalid/tsconfig.json index d8ff04c12..7f2bd50c5 100644 --- a/test/fixtures/build-invalid/tsconfig.json +++ b/test/fixtures/build-invalid/tsconfig.json @@ -4,7 +4,7 @@ "lib": ["dom", "esnext"], "declaration": true, "sourceMap": true, - "rootDir": "./", + "rootDir": "./src", "strict": true, "noImplicitAny": true, "strictNullChecks": true, diff --git a/test/fixtures/build-withConfig/tsconfig.json b/test/fixtures/build-withConfig/tsconfig.json index 43e3ec95f..a8c8432ed 100644 --- a/test/fixtures/build-withConfig/tsconfig.json +++ b/test/fixtures/build-withConfig/tsconfig.json @@ -4,7 +4,7 @@ "lib": ["dom", "esnext"], "declaration": true, "sourceMap": true, - "rootDir": "./", + "rootDir": "./src", "strict": true, "noImplicitAny": true, "strictNullChecks": true,