-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcopy.js
68 lines (56 loc) · 1.54 KB
/
copy.js
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
/**
* This file only purpose is to copy files before npm publish and strip churn/security sensitive metadata from package.json
*
* **NOTE:**
* 👉 This file should not use any 3rd party dependency
*/
const { writeFileSync, copyFileSync, statSync } = require('fs')
const { resolve, basename } = require('path')
const packageJson = require('../package.json')
main()
function main() {
const projectRoot = resolve(__dirname, '..')
const distPath = resolve(projectRoot, 'dist')
const distPackageJson = createDistPackageJson(packageJson)
const cpFiles = ['README.md', 'CHANGELOG.md', 'LICENSE.md', '.npmignore'].map(
(file) => resolve(projectRoot, file)
)
cp(cpFiles, distPath)
writeFileSync(resolve(distPath, 'package.json'), distPackageJson)
}
/**
*
* @param {string[]|string} source
* @param {string} target
*/
function cp(source, target) {
const isDir = statSync(target).isDirectory()
if (isDir) {
if (!Array.isArray(source)) {
throw new Error(
'if <target> is directory you need to provide source as an array'
)
}
source.forEach((file) =>
copyFileSync(file, resolve(target, basename(file)))
)
return
}
copyFileSync(/** @type {string} */ (source), target)
}
/**
* @param {typeof packageJson} packageConfig
* @return {string}
*/
function createDistPackageJson(packageConfig) {
const {
devDependencies,
scripts,
engines,
config,
husky,
'lint-staged': lintStaged,
...distPackageJson
} = packageConfig
return JSON.stringify(distPackageJson, null, 2)
}