forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclear-package-dir.js
164 lines (145 loc) · 4.09 KB
/
clear-package-dir.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const ignore = require(`ignore`)
const fs = require(`fs-extra`)
const yargs = require(`yargs`)
const chalk = require(`chalk`)
const collectUpdates = require(`@lerna/collect-updates`)
const PackageGraph = require(`@lerna/package-graph`)
const Project = require(`@lerna/project`)
const PromptUtilities = require(`@lerna/prompt`)
const _ = require(`lodash`)
const path = require(`path`)
const packlist = require(`npm-packlist`)
const { execSync } = require(`child_process`)
let argv = yargs
.command(
`* <bump>`,
`Clear previously built and potentially stale files in packages`,
commandBuilder => {
commandBuilder.positional(`bump`, {
choices: [
`major`,
`minor`,
`patch`,
`premajor`,
`preminor`,
`prepatch`,
`prerelease`,
],
})
}
)
.option(`dry-run`, {
default: false,
describe: `Don't delete files - just show what would be deleted`,
})
.option(`verbose`, {
default: false,
describe: `Show files that would be bundled and mark files that will be deleted`,
})
.option(`force`, {
default: false,
describe: `Force deletion of file without prompting user to confirm`,
}).argv
const verbose = argv[`dry-run`] || argv[`verbose`] || !argv[`force`]
const buildIgnoreArray = str =>
str
.split(`\n`)
.filter(line => {
// skip empty lines and comments
if (!line || line[0] === `#`) {
return false
}
return true
})
.reduce((acc, item) => {
acc.push(item)
// add "<directory>/**" glob as ignore package need that to
// properly ignore entries like "node_modules"
if (!/\*\*$/.test(item)) {
acc.push(`${item}/**`)
}
return acc
}, [])
const getListOfFilesToClear = async ({ location, name }) => {
let gitignore = []
try {
gitignore = buildIgnoreArray(
fs.readFileSync(path.join(location, `.gitignore`), `utf-8`)
)
} catch {
// not all packages have .gitignore - see gatsby-plugin-no-sourcemap
} finally {
const notTrackedFiles = execSync(
`git ls-files --others --exclude-standard`,
{
cwd: location,
}
)
.toString()
.split(`\n`)
gitignore = gitignore.concat(notTrackedFiles)
}
const result = await packlist({ path: location })
const ig = ignore().add(gitignore)
if (verbose) {
console.log(`Files that will be packed for ${chalk.bold(name)}:`)
}
const filesToDelete = result
.filter(file => {
const willBeDeleted = ig.ignores(file)
if (verbose) {
console.log(
`[ ${
willBeDeleted ? chalk.red(`DEL`) : chalk.green(` - `)
} ] ${path.posix.join(file)}`
)
}
return willBeDeleted
})
.map(file => path.join(location, file))
return filesToDelete
}
const run = async () => {
try {
const project = new Project(process.cwd())
const packages = await project.getPackages()
const packageGraph = new PackageGraph(packages)
const changed = collectUpdates(
packageGraph.rawPackageList,
packageGraph,
{ cwd: process.cwd() },
{
...project.config,
loglevel: `silent`,
bump: argv.bump,
}
)
const filesToDelete = _.flatten(
await Promise.all(changed.map(getListOfFilesToClear))
)
if (!argv[`dry-run`] && filesToDelete.length > 0) {
if (
argv[`force`] ||
(await PromptUtilities.confirm(
`Are you sure you want to delete those files?`
))
) {
filesToDelete.forEach(file => {
fs.removeSync(file)
})
} else {
console.log(
`${chalk.red(
`Stopping publish`
)}: there are files that need to be cleared.\n\nIf this is a bug in check script and everything is fine, run:\n\n${chalk.green(
`yarn lerna publish`
)}\n\ndirectly to skip checks (and hopefully apply changes to clear-package-dir script to fix it).`
)
process.exit(1)
}
}
} catch {
// if no packages are marked as changed, lerna will exit with non-zero
}
}
run()