-
Notifications
You must be signed in to change notification settings - Fork 18
/
clean-publish.js
95 lines (91 loc) · 2.76 KB
/
clean-publish.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
const chalk = require('chalk')
const fs = require('fs')
const spawn = require('cross-spawn')
const fse = require('fs-extra')
const omit = require('lodash.omit')
const pick = require('lodash.pick')
const yargs = require('yargs')
const { regExpIndexOf, multiCp } = require('./utils')
const IGNORE_FILES = require('./ignore-files')
const IGNORE_FIELDS = require('./ignore-fields')
const NPM_SCRIPTS = require('./npm-scripts')
const { argv } = yargs
.usage('$0')
.option('files', {
type: 'array',
desc: 'One or more exclude files'
})
.option('fields', {
type: 'array',
desc: 'One or more exclude package.json fields'
});
(function () {
const src = './'
const packageJSON = 'package.json'
fs.mkdtemp('tmp', (makeTmpDirErr, tmpDir) => {
if (makeTmpDirErr) {
console.error(chalk.red(makeTmpDirErr))
process.exit()
}
fs.readdir(src, (readSrcDirErr, files) => {
if (readSrcDirErr) {
console.error(chalk.red(readSrcDirErr))
process.exit()
}
const ignoreFiles = argv.files
? IGNORE_FILES.concat(argv.files)
: IGNORE_FILES
const filteredFiles = files.filter(file => (
file !== tmpDir && regExpIndexOf(ignoreFiles, file) === -1
))
multiCp(filteredFiles.map(file => ({
from: `${ src }${ file }`,
to: `${ tmpDir }/${ file }`
})))
.then(() => {
fse.readJson(packageJSON, (err, obj) => {
if (err) {
console.error(chalk.red(err))
process.exit()
}
const ignoreFields = argv.fields
? IGNORE_FIELDS.concat(argv.fields)
: IGNORE_FIELDS
const modifiedPackageJSON = Object.assign(
omit(obj, ignoreFields),
{
scripts: pick(obj.scripts, NPM_SCRIPTS)
}
)
fse.writeJson(
`./${ tmpDir }/${ packageJSON }`,
modifiedPackageJSON,
{ spaces: 2 },
writePackageJSONErr => {
if (writePackageJSONErr) {
console.error(chalk.red(writePackageJSONErr))
process.exit()
}
spawn('npm', ['publish'], {
stdio: 'inherit',
cwd: tmpDir
}).on('close', () => {
fse.remove(tmpDir, removeTmpDirErr => {
if (removeTmpDirErr) {
console.error(chalk.red(removeTmpDirErr))
process.exit()
}
})
})
}
)
})
})
.catch(copyErr => {
console.error(chalk.red(copyErr))
process.exit()
})
})
})
})()