Skip to content

Commit f82e6dc

Browse files
authored
patch(commands): Delete unwated built files
When running babel we copy over some JS & JSON files we don't need to transpile, but as a result the test files get copied over to built locations which we don't want. So now after finishing with the transpiling and generation of type declarations, we delete test files (either in `__tests__` folder or with `.spec` extension).
1 parent 3496718 commit f82e6dc

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed

.npmignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"jest-runner-tsc": "^1.3.2",
5959
"jest-watch-typeahead": "^0.3.0",
6060
"lodash": "^4.17.11",
61+
"rimraf": "^2.6.3",
6162
"typescript": "^3.2.1",
6263
"yargs": "^13.1.0"
6364
},

src/commands/build/__tests__/utils.spec.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
import {resolve} from 'path'
2-
import {getBabelArgs, getTypescriptArgs} from '../utils'
2+
import {
3+
getBabelArgs,
4+
getTypescriptArgs,
5+
getCopiedFilesToDelete,
6+
} from '../utils'
37
import {BUILD_ARGS} from '../../../cli/args'
48
import {ModuleFormat} from '../../types'
59
import BASE_TSCONFIG from '../../test/tsconfig.json'
610

711
const CWD = process.cwd()
812

913
describe('getBabelArgs', () => {
14+
describe('source files', () => {
15+
it('uses current working directory for source files location', () => {
16+
const [firstSetOfArgs] = getBabelArgs({
17+
formats: BUILD_ARGS.formats.default,
18+
out: BUILD_ARGS.out.default,
19+
watch: BUILD_ARGS.watch.default,
20+
})
21+
22+
expect(firstSetOfArgs.cliOptions.filenames).toEqual([resolve(CWD, 'src')])
23+
})
24+
})
25+
1026
describe('formats + out', () => {
1127
it('returns empty array when empty formats are specified', () => {
1228
const babelArgsToRun = getBabelArgs({
@@ -195,14 +211,34 @@ describe('getTypescriptArgs', () => {
195211
})
196212
})
197213

198-
it('uses current working director for source files location', () => {
199-
const typescriptArgs = getTypescriptArgs({
200-
formats: new Set(['type'] as ModuleFormat[]),
201-
out: BUILD_ARGS.out.default,
202-
watch: BUILD_ARGS.watch.default,
214+
describe('source files', () => {
215+
it('uses current working directory for source files location', () => {
216+
const typescriptArgs = getTypescriptArgs({
217+
formats: new Set(['type'] as ModuleFormat[]),
218+
out: BUILD_ARGS.out.default,
219+
watch: BUILD_ARGS.watch.default,
220+
})
221+
222+
expect(typescriptArgs).toContain(resolve(CWD, 'src/*.ts'))
203223
})
224+
})
225+
})
226+
})
227+
228+
describe('getCopiedFilesToDelete', () => {
229+
it('prepends the passed in output path to the glob patterns (when relative)', () => {
230+
const files = getCopiedFilesToDelete('./built')
231+
232+
files.forEach((globPattern) => {
233+
expect(globPattern).toMatch(new RegExp(`^${resolve(CWD, 'built')}/`))
234+
})
235+
})
236+
237+
it('prepends the passed in output path to the glob patterns (when absolute)', () => {
238+
const files = getCopiedFilesToDelete('/path/to/built')
204239

205-
expect(typescriptArgs).toContain(resolve(CWD, 'src/*.ts'))
240+
files.forEach((globPattern) => {
241+
expect(globPattern).toMatch(new RegExp('/path/to/built/'))
206242
})
207243
})
208244
})

src/commands/build/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import {exec} from 'child_process'
33
import runBabel from './run-babel'
44
import {BUILD_ARGS} from '../../cli/args'
55
import {Result, ModuleFormat} from '../types'
6-
import {getBabelArgs, getTypescriptArgs} from './utils'
6+
import {
7+
getBabelArgs,
8+
getTypescriptArgs,
9+
getCopiedFilesToDelete,
10+
} from './utils'
711

812
const execAsync = promisify(exec)
913

@@ -44,6 +48,9 @@ export default async ({
4448
throw Error('Unable able to generate type definitions')
4549
}
4650
}
51+
52+
// remove all of the copied files that we don't want in built directory
53+
await execAsync(`npx rimraf ${getCopiedFilesToDelete(out).join(' ')}`)
4754
} catch (error) {
4855
return {
4956
code: error.code || 1,

src/commands/build/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ interface BuildArgs {
5757

5858
const VALID_BABEL_FORMATS = new Set(['cjs', 'esm'] as ModuleFormat[])
5959

60+
const BUILT_FILES_TO_REMOVE = [
61+
'**/__tests__/',
62+
'**/*.spec.*',
63+
]
64+
6065
/**
6166
* Gets an array of options/arguments to pass babel, one for each valid format
6267
* @param {BuildArgs} options
@@ -117,3 +122,12 @@ export const getTypescriptArgs = ({formats, out, watch}: BuildArgs): string[] |
117122

118123
return args
119124
}
125+
126+
/**
127+
* Gets list of glob patterns of copied files that should be deleted after building
128+
* @param {string} outputPath A path (relative or absolute) to the output directory for the type definitions
129+
* @returns {string[]}
130+
*/
131+
export const getCopiedFilesToDelete = (outputPath: string): string[] => (
132+
BUILT_FILES_TO_REMOVE.map((glob) => resolve(`${outputPath}/${glob}`))
133+
)

0 commit comments

Comments
 (0)