Skip to content

Commit 68f26c9

Browse files
authored
(fix): set rootDir to './src', not './'. deprecate moveTypes (#504)
* (fix/refactor): rewrite some overbroad try/catches - so there's less silent failures that occur but don't error - replace overbroad try/catch in getProjectPath with just an fs.pathExists - replace overbroad try/catch in cleanDistFolder with just an fs.remove - fs.remove is like rimraf and `rm -rf` in that it won't error if the file/dir doesn't exist - if it does error, it's probably because it *failed* to remove the dir, and that should error, because it's potentially a problem, especially if you're publishing right after - rewrite moveTypes() so it doesn't have an overbroad try/catch - use fs.pathExists first and early return if it doesn't exist - only check for known errors with fs.copy, and rethrow others - this way if copy or remove actually fail, they will actually error - before they would silently fail, which could similarly be pretty bad if one were to publish right after a silent failure * (fix): set rootDir to './src', not './'. deprecate moveTypes - rootDir needed to be changed to ./src because the previous ./ caused type declarations to be generated in dist/src/ instead of just dist/ - the moveTypes function handled moving the declarations back into dist/, but occassionally had errors moving .d.ts files - particularly in CI and for projects with many of them - declarationMap (*.d.ts.map) files would also have issues due to the hackiness of moveTypes, setting to rootDir to './src' is one of the necessary steps in fixing them - deprecate moveTypes and add a warning with instructions if it is used when a rootDir of './' is detected - add notes about a deprecation window in the comments * (empty/removeme): test CI again 1 * (empty/removeme): test CI again 2 * (empty/removeme): test CI again 3 * (empty/removeme): test CI again 4 * (empty/removeme): test CI again 5 * (empty/removeme): test CI again 6 * (empty/removeme): test CI again 7 * (empty/removeme): test CI again 8 * (empty/removeme): test CI again 9 * (empty/removeme): test CI again 10 * (empty/removeme): test CI again 11 * (empty/removeme): test CI again 12 * (empty/removeme): test CI again 13 * (empty/removeme): test CI again 14 * (empty/removeme): test CI again 15 * (empty/removeme): test CI again 16 * (empty/removeme): test CI again 17 * (empty/removeme): test CI again 18 * (empty/removeme): test CI again 19 * (empty/removeme): test CI again 20 * (empty/removeme): test CI again 21 * (empty/removeme): test CI again 22 * (empty/removeme): test CI again 23 * (empty/removeme): test CI again 24 * (empty/removeme): test CI again 25 * (empty/removeme): test CI again 26 * (empty/removeme): test CI again 27 * (empty/removeme): test CI again 28 * (empty/removeme): test CI again 29 * (empty/removeme): test CI again 30 * (empty/removeme): test CI again 31 * (empty/removeme): test CI again 32 * (empty/removeme): test CI again 33 * more descriptive warning about bugs, fixup with (fix): set rootDir to './src', not './'. deprecate moveTypes * (empty/removeme): test CI again 34 * (empty/removeme): test CI again 35 * (empty/removeme): test CI again 36 * (empty/removeme): test CI again 37 * (empty/removeme): test CI again 38 * add a comment that the catch is the problem, fixup with (fix): set rootDir to './src', not './'. deprecate moveTypes
1 parent a97a5af commit 68f26c9

File tree

8 files changed

+58
-35
lines changed

8 files changed

+58
-35
lines changed

src/deprecated.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as fs from 'fs-extra';
2+
3+
import { paths } from './constants';
4+
5+
/*
6+
This was originally needed because the default
7+
tsconfig.compilerOptions.rootDir was set to './' instead of './src'.
8+
Now that it's set to './src', this is now deprecated.
9+
To ensure a stable upgrade path for users, leave the warning in for
10+
6 months - 1 year, then change it to an error in a breaking bump and leave
11+
that in for some time too.
12+
*/
13+
export async function moveTypes() {
14+
const appDistSrc = paths.appDist + '/src';
15+
16+
const pathExists = await fs.pathExists(appDistSrc);
17+
if (!pathExists) return;
18+
19+
// see note above about deprecation window
20+
console.warn(
21+
'[tsdx]: Your rootDir is currently set to "./". Please change your ' +
22+
'rootDir to "./src".\n' +
23+
'TSDX has deprecated setting tsconfig.compilerOptions.rootDir to ' +
24+
'"./" as it caused buggy output for declarationMaps and occassionally ' +
25+
'for type declarations themselves.'
26+
);
27+
28+
try {
29+
// Move the typescript types to the base of the ./dist folder
30+
await fs.copy(appDistSrc, paths.appDist, {
31+
overwrite: true,
32+
});
33+
} catch (err) {
34+
// ignore errors about the destination dir already existing or files not
35+
// existing as those always occur for some reason, re-throw any other
36+
// unexpected failures
37+
// NOTE: these errors mean that sometimes files don't get moved properly,
38+
// meaning that it's buggy / unreliable (see console.warn above)
39+
if (err.code !== 'EEXIST' && err.code !== 'ENOENT') {
40+
throw err;
41+
}
42+
}
43+
44+
await fs.remove(appDistSrc);
45+
}

src/index.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ import {
1212
} from 'rollup';
1313
import asyncro from 'asyncro';
1414
import chalk from 'chalk';
15-
import util from 'util';
1615
import * as fs from 'fs-extra';
1716
import jest from 'jest';
1817
import { CLIEngine } from 'eslint';
1918
import logError from './logError';
2019
import path from 'path';
21-
import rimraf from 'rimraf';
2220
import execa from 'execa';
2321
import shell from 'shelljs';
2422
import ora from 'ora';
@@ -48,6 +46,7 @@ import {
4846
import { createProgressEstimator } from './createProgressEstimator';
4947
import { templates } from './templates';
5048
import { composePackageJson } from './templates/utils';
49+
import * as deprecated from './deprecated';
5150
const pkg = require('../package.json');
5251

5352
const prog = sade('tsdx');
@@ -101,16 +100,6 @@ async function getInputs(
101100
return concatAllArray(inputs);
102101
}
103102

104-
async function moveTypes() {
105-
try {
106-
// Move the typescript types to the base of the ./dist folder
107-
await fs.copy(paths.appDist + '/src', paths.appDist, {
108-
overwrite: true,
109-
});
110-
await fs.remove(paths.appDist + '/src');
111-
} catch (e) {}
112-
}
113-
114103
prog
115104
.version(pkg.version)
116105
.command('create <pkg>')
@@ -140,16 +129,11 @@ prog
140129
// Helper fn to prompt the user for a different
141130
// folder name if one already exists
142131
async function getProjectPath(projectPath: string): Promise<string> {
143-
let exists = true;
144-
try {
145-
// will throw an exception if it does not exists
146-
await util.promisify(fs.access)(projectPath);
147-
} catch {
148-
exists = false;
149-
}
132+
const exists = await fs.pathExists(projectPath);
150133
if (!exists) {
151134
return projectPath;
152135
}
136+
153137
bootSpinner.fail(`Failed to create ${chalk.bold.red(pkg)}`);
154138
const prompt = new Input({
155139
message: `A folder named ${chalk.bold.red(
@@ -158,6 +142,7 @@ prog
158142
initial: pkg + '-1',
159143
result: (v: string) => v.trim(),
160144
});
145+
161146
pkg = await prompt.run();
162147
projectPath = (await fs.realpath(process.cwd())) + '/' + pkg;
163148
bootSpinner.start(`Creating ${chalk.bold.green(pkg)}...`);
@@ -373,7 +358,7 @@ prog
373358
`);
374359

375360
try {
376-
await moveTypes();
361+
await deprecated.moveTypes();
377362

378363
if (firstTime && opts.onFirstSuccess) {
379364
firstTime = false;
@@ -424,7 +409,7 @@ prog
424409
async (inputOptions: RollupOptions & { output: OutputOptions }) => {
425410
let bundle = await rollup(inputOptions);
426411
await bundle.write(inputOptions.output);
427-
await moveTypes();
412+
await deprecated.moveTypes();
428413
}
429414
)
430415
.catch((e: any) => {
@@ -453,14 +438,7 @@ async function normalizeOpts(opts: WatchOpts): Promise<NormalizedOpts> {
453438
}
454439

455440
async function cleanDistFolder() {
456-
try {
457-
await util.promisify(fs.access)(paths.appDist);
458-
return util.promisify(rimraf)(paths.appDist);
459-
} catch {
460-
// if an exception is throw, the files does not exists or it is not visible
461-
// either way, we just return
462-
return;
463-
}
441+
await fs.remove(paths.appDist);
464442
}
465443

466444
function writeCjsEntryFile(name: string) {

templates/basic/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"importHelpers": true,
77
"declaration": true,
88
"sourceMap": true,
9-
"rootDir": "./",
9+
"rootDir": "./src",
1010
"strict": true,
1111
"noImplicitAny": true,
1212
"strictNullChecks": true,

templates/react-with-storybook/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"importHelpers": true,
77
"declaration": true,
88
"sourceMap": true,
9-
"rootDir": "./",
9+
"rootDir": "./src",
1010
"strict": true,
1111
"noImplicitAny": true,
1212
"strictNullChecks": true,

templates/react/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"importHelpers": true,
77
"declaration": true,
88
"sourceMap": true,
9-
"rootDir": "./",
9+
"rootDir": "./src",
1010
"strict": true,
1111
"noImplicitAny": true,
1212
"strictNullChecks": true,

test/fixtures/build-default/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"lib": ["dom", "esnext"],
55
"declaration": true,
66
"sourceMap": true,
7-
"rootDir": "./",
7+
"rootDir": "./src",
88
"strict": true,
99
"noImplicitAny": true,
1010
"strictNullChecks": true,

test/fixtures/build-invalid/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"lib": ["dom", "esnext"],
55
"declaration": true,
66
"sourceMap": true,
7-
"rootDir": "./",
7+
"rootDir": "./src",
88
"strict": true,
99
"noImplicitAny": true,
1010
"strictNullChecks": true,

test/fixtures/build-withConfig/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"lib": ["dom", "esnext"],
55
"declaration": true,
66
"sourceMap": true,
7-
"rootDir": "./",
7+
"rootDir": "./src",
88
"strict": true,
99
"noImplicitAny": true,
1010
"strictNullChecks": true,

0 commit comments

Comments
 (0)