-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathprepare-dist.js
More file actions
executable file
·91 lines (79 loc) · 2.68 KB
/
prepare-dist.js
File metadata and controls
executable file
·91 lines (79 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/* istanbul ignore file */
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs')
const fsPromises = require('fs').promises
const path = require('path')
const replace = require('replace-in-file')
const packlist = require('npm-packlist')
const {copyFile} = fsPromises
const DEST_DIR = 'dist/'
/**
* Error catcher that will log an error and exit the process
* @private
*/
const catcher = (message) => (error) => {
console.log(`${message}: ${error}`)
process.exit(1)
}
/**
* Copy files from their source to a given destination.
* @private
* @param srcs {Array<String>} any array of file paths
* @param dest {String} the destination path
* @returns {Promise} resolves when files are copied.
*/
const copyFiles = (srcs, dest) => {
return Promise.all(
srcs.map((src) => {
// Assign the full file path
const newFilePath = path.join(dest, src)
// Ensure the path exists, create if it doesn't.
if (!fs.existsSync(newFilePath)) {
fs.mkdirSync(path.dirname(newFilePath), {recursive: true})
}
return copyFile(src, path.join(dest, src))
})
)
}
const main = async () => {
console.log('Preparing dist...')
// Remove the dist/package.json so we don't end up including more files in
// the package.
await fsPromises.rm(`${DEST_DIR}/package.json`, {force: true})
try {
// Get a list of files from the `npm pack --dry-run` command.
const packageFiles = (await packlist()).filter((path) => !path.startsWith('dist/'))
// Move the required files into the `dist` folder.
await copyFiles(packageFiles, DEST_DIR)
} catch (e) {
catcher('Error while copying files')(e)
}
try {
// Update package.json imports.
await replace({
ignore: ['dist/scripts/**/*', 'dist/bin/**/*', 'dist/template/**/*'],
files: ['dist/**/*.js'],
from: /..\/package.json/g,
to: 'package.json'
})
// Update script to remove `dist` folder in imports.
await replace({
files: ['dist/scripts/**/!(prepare-dist.js)'],
from: /dist\//g,
to: '',
// Scripts are optional, don't fail if nothing matches the glob.
allowEmptyPaths: true
})
} catch (e) {
catcher('Error replacing file references')(e)
}
console.log('Successfully prepared!')
}
main()