forked from shashkovdanil/clean-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-publish.js
111 lines (106 loc) · 2.59 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env node
const path = require('path')
const chalk = require('chalk')
const yargs = require('yargs')
const {
createTempDirectory,
readSrcDirectory,
clearFilesList,
copyFiles,
readPackageJSON,
clearPackageJSON,
writePackageJSON,
publish,
removeTempDirectory,
runScript
} = require('./core')
const getConfig = require('./get-config')
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'
})
.option('without-publish', {
type: 'boolean',
desc: 'Clean package without npm publish'
})
.option('package-manager', {
types: 'string',
default: 'npm',
desc: 'Package manager to use'
})
.option('access', {
types: 'string',
desc: 'Whether the npm registry publishes ' +
'this package as a public package, or restricted'
})
.option('before-script', {
types: 'string',
desc: 'Run script on the to-release dir before npm publish'
})
const options = {}
let tempDirectoryName
let isPrepublishSuccess
function handleOptions () {
Object.assign(options, argv, {
withoutPublish: argv['without-publish'],
packageManager: argv['package-manager']
})
if (options['_'].length === 0) {
return getConfig().then(config => {
Object.assign(options, config)
})
}
return Promise.resolve()
}
handleOptions()
.then(() => (
createTempDirectory()
))
.then(directoryName => {
tempDirectoryName = directoryName
return readSrcDirectory()
})
.then(files => {
const filteredFiles = clearFilesList(
files,
[tempDirectoryName].concat(options.files)
)
return copyFiles(filteredFiles, tempDirectoryName)
})
.then(() => (
readPackageJSON()
))
.then(packageJson => {
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields)
return writePackageJSON(tempDirectoryName, cleanPackageJSON)
})
.then(() => {
if (options.beforeScript) {
const dirPath = path.join(__dirname, tempDirectoryName)
const { code } = runScript(options.beforeScript, dirPath)
isPrepublishSuccess = (code === 0)
}
})
.then(() => {
if (!options.withoutPublish) {
if (options.beforeScript && !isPrepublishSuccess) {
return
}
return publish(tempDirectoryName, options.packageManager, options.access)
}
})
.then(() => {
if (!options.withoutPublish) {
return removeTempDirectory(tempDirectoryName)
}
})
.catch(error => {
console.error(chalk.red(error))
process.exit()
})