-
Notifications
You must be signed in to change notification settings - Fork 18
/
clean-publish.js
executable file
·139 lines (123 loc) · 4.24 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
import {
cleanComments,
cleanDocs,
clearPackageJSON,
copyFiles,
createFilesFilter,
createTempDirectory,
publish,
readPackageJSON,
removeTempDirectory,
runScript,
writePackageJSON
} from './core.js'
import { getConfig } from './get-config.js'
import { parseListArg } from './utils.js'
const HELP =
'npx clean-publish\n' +
'\n' +
'Options:\n' +
' --help Show help\n' +
' --version Show version number\n' +
' --clean-docs Keep only main section of README.md' +
' --clean-comments Clean inline comments from JS files' +
' --files One or more exclude files\n' +
' --fields One or more exclude package.json fields\n' +
' --without-publish Clean package without npm publish\n' +
' --dry-run Reports the details of what would have been published\n' +
' --package-manager Package manager to use\n' +
' --access Whether the npm registry publishes this package\n' +
' as a public package, or restricted\n' +
' --tag Registers the package with the given tag\n' +
' --before-script Run script on the to-release dir before npm\n' +
' publish\n' +
' --temp-dir Create temporary directory with given name\n' +
' -- Pass next arguments as package manager options'
const DEFAULT_OPTIONS = {
packageManager: 'npm'
}
async function handleOptions() {
let options = {}
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i] === '--help') {
process.stdout.write(HELP + '\n')
process.exit(0)
} else if (process.argv[i] === '--version') {
process.stdout.write(require('./package.json').version + '\n')
process.exit(0)
} else if (process.argv[i] === '--without-publish') {
options.withoutPublish = true
} else if (process.argv[i] === '--dry-run') {
options.dryRun = true
i += 1
} else if (process.argv[i] === '--package-manager') {
options.packageManager = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--before-script') {
options.beforeScript = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--access') {
options.access = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--files') {
options.files = parseListArg(process.argv[i + 1])
i += 1
} else if (process.argv[i] === '--clean-docs') {
options.cleanDocs = true
i += 1
} else if (process.argv[i] === '--clean-comments') {
options.cleanComments = true
i += 1
} else if (process.argv[i] === '--tag') {
options.tag = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--fields') {
options.fields = parseListArg(process.argv[i + 1])
i += 1
} else if (process.argv[i] === '--temp-dir') {
options.tempDir = process.argv[i + 1]
i += 1
} else if (process.argv[i] === '--') {
options.packageManagerOptions = process.argv.slice(i + 1)
break
} else {
options._ = process.argv[i]
}
}
if (!options._) {
let config = await getConfig()
return { ...DEFAULT_OPTIONS, ...config, ...options }
} else {
return options
}
}
async function run() {
const options = await handleOptions()
const tempDirectoryName = await createTempDirectory(options.tempDir)
const filesFilter = createFilesFilter(options.files)
await copyFiles(tempDirectoryName, filesFilter)
const packageJson = await readPackageJSON()
if (options.cleanDocs) {
await cleanDocs(tempDirectoryName, packageJson.repository, packageJson.homepage)
}
if (options.cleanComments) {
await cleanComments(tempDirectoryName)
}
const cleanPackageJSON = clearPackageJSON(packageJson, options.fields)
await writePackageJSON(tempDirectoryName, cleanPackageJSON)
let prepublishSuccess = true
if (options.beforeScript) {
prepublishSuccess = await runScript(options.beforeScript, tempDirectoryName)
}
if (!options.withoutPublish && prepublishSuccess) {
await publish(tempDirectoryName, options)
}
if (!options.withoutPublish) {
await removeTempDirectory(tempDirectoryName)
}
}
run().catch(error => {
process.stderr.write(error.stack + '\n')
process.exit(1)
})