diff --git a/package.json b/package.json index 04acc287b..13bd7b701 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@babel/plugin-transform-runtime": "^7.6.0", "@babel/polyfill": "^7.4.4", "@babel/preset-env": "^7.4.4", + "@types/rimraf": "^2.0.2", "@typescript-eslint/eslint-plugin": "^2.3.1", "@typescript-eslint/parser": "^2.3.1", "@wessberg/rollup-plugin-ts": "^1.1.65", @@ -77,6 +78,7 @@ "pascal-case": "^2.0.1", "prettier": "^1.18.2", "progress-estimator": "^0.2.2", + "rimraf": "^3.0.0", "rollup": "^1.12.0", "rollup-plugin-babel": "^4.3.2", "rollup-plugin-commonjs": "^10.0.0", diff --git a/src/index.ts b/src/index.ts index 5f28212e3..0952efab8 100755 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,7 @@ import { CLIEngine } from 'eslint'; import logError from './logError'; import path from 'path'; import mkdirp from 'mkdirp'; +import rimraf from 'rimraf'; import execa from 'execa'; import ora from 'ora'; import { paths } from './constants'; @@ -333,6 +334,7 @@ prog .action(async (dirtyOpts: any) => { const opts = await normalizeOpts(dirtyOpts); const buildConfigs = createBuildConfigs(opts); + await cleanDistFolder(); await ensureDistFolder(); if (opts.format.includes('cjs')) { await writeCjsEntryFile(opts.name); @@ -397,6 +399,7 @@ prog .action(async (dirtyOpts: any) => { const opts = await normalizeOpts(dirtyOpts); const buildConfigs = createBuildConfigs(opts); + await cleanDistFolder(); await ensureDistFolder(); if (opts.format.includes('cjs')) { const promise = writeCjsEntryFile(opts.name).catch(logError); @@ -441,6 +444,12 @@ function ensureDistFolder() { return util.promisify(mkdirp)(resolveApp('dist')); } +function cleanDistFolder() { + if (fs.existsSync(paths.appDist)) { + return util.promisify(rimraf)(paths.appDist); + } +} + function writeCjsEntryFile(name: string) { const baseLine = `module.exports = require('./${safePackageName(name)}`; const contents = ` diff --git a/test/fixtures/build-default/package2.json b/test/fixtures/build-default/package2.json new file mode 100644 index 000000000..6a6ee8983 --- /dev/null +++ b/test/fixtures/build-default/package2.json @@ -0,0 +1,7 @@ +{ + "scripts": { + "build": "tsdx build" + }, + "name": "build-default-2", + "license": "MIT" +} diff --git a/test/tests/tsdx-build.test.js b/test/tests/tsdx-build.test.js index 2ffdcedf1..7c34af7c5 100644 --- a/test/tests/tsdx-build.test.js +++ b/test/tests/tsdx-build.test.js @@ -43,6 +43,42 @@ describe('tsdx build', () => { expect(lib.foo()).toBe('bar'); }); + it('should clean the dist directory before rebuilding', () => { + util.setupStageWithFixture(stageName, 'build-default'); + + shell.mv('package.json', 'package-og.json'); + shell.mv('package2.json', 'package.json'); + + const output = shell.exec('node ../dist/index.js build --format esm,cjs'); + expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); + + // build-default files have been cleaned out + expect( + shell.test('-f', 'dist/build-default.cjs.development.js') + ).toBeFalsy(); + expect( + shell.test('-f', 'dist/build-default.cjs.production.min.js') + ).toBeFalsy(); + expect(shell.test('-f', 'dist/build-default.esm.js')).toBeFalsy(); + + // build-default-2 files have been added + expect( + shell.test('-f', 'dist/build-default-2.cjs.development.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-default-2.cjs.production.min.js') + ).toBeTruthy(); + expect(shell.test('-f', 'dist/build-default-2.esm.js')).toBeTruthy(); + + expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); + + expect(output.code).toBe(0); + + // reset package.json files + shell.mv('package.json', 'package2.json'); + shell.mv('package-og.json', 'package.json'); + }); + it('should fail gracefully with exit code 1 when build failed', () => { util.setupStageWithFixture(stageName, 'build-invalid'); const code = shell.exec('node ../dist/index.js build').code; diff --git a/yarn.lock b/yarn.lock index 8b8eebbb3..855e5962b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -903,6 +903,11 @@ resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw== +"@types/events@*": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" + integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== + "@types/execa@^0.9.0": version "0.9.0" resolved "https://registry.yarnpkg.com/@types/execa/-/execa-0.9.0.tgz#9b025d2755f17e80beaf9368c3f4f319d8b0fb93" @@ -917,6 +922,15 @@ dependencies: "@types/node" "*" +"@types/glob@*": + version "7.1.1" + resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575" + integrity sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w== + dependencies: + "@types/events" "*" + "@types/minimatch" "*" + "@types/node" "*" + "@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0": version "2.0.1" resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.1.tgz#42995b446db9a48a11a07ec083499a860e9138ff" @@ -954,6 +968,11 @@ resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636" integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A== +"@types/minimatch@*": + version "3.0.3" + resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" + integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA== + "@types/mkdirp@^0.5.2": version "0.5.2" resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-0.5.2.tgz#503aacfe5cc2703d5484326b1b27efa67a339c1f" @@ -1008,6 +1027,14 @@ dependencies: "@types/node" "*" +"@types/rimraf@^2.0.2": + version "2.0.2" + resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-2.0.2.tgz#7f0fc3cf0ff0ad2a99bb723ae1764f30acaf8b6e" + integrity sha512-Hm/bnWq0TCy7jmjeN5bKYij9vw5GrDFWME4IuxV08278NtU/VdGbzsBohcCUJ7+QMqmUq5hpRKB39HeQWJjztQ== + dependencies: + "@types/glob" "*" + "@types/node" "*" + "@types/rollup-plugin-json@^3.0.2": version "3.0.2" resolved "https://registry.yarnpkg.com/@types/rollup-plugin-json/-/rollup-plugin-json-3.0.2.tgz#1153136a515ed4fbb7ef214ace496f5fc3ed7796" @@ -6043,6 +6070,13 @@ rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.3: dependencies: glob "^7.1.3" +rimraf@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.0.tgz#614176d4b3010b75e5c390eb0ee96f6dc0cebb9b" + integrity sha512-NDGVxTsjqfunkds7CqsOiEnxln4Bo7Nddl3XhS4pXg5OzwkLqJ971ZVAAnB+DDLnF76N+VnDEiBHaVV8I06SUg== + dependencies: + glob "^7.1.3" + ripemd160@^2.0.0, ripemd160@^2.0.1: version "2.0.2" resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-2.0.2.tgz#a1c1a6f624751577ba5d07914cbc92850585890c"