-
Notifications
You must be signed in to change notification settings - Fork 964
/
tarsync.mjs
178 lines (138 loc) · 4.32 KB
/
tarsync.mjs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node
/* eslint-env node */
import { performance } from 'node:perf_hooks'
import { fileURLToPath } from 'node:url'
import { parseArgs as nodeUtilParseArgs } from 'node:util'
import ora from 'ora'
import { cd, chalk, fs, glob, path, within, $ } from 'zx'
const mockSpinner = {
text: '',
succeed: () => {},
}
async function main() {
let options
try {
options = await parseArgs()
} catch (e) {
console.error(e.message)
process.exitCode = 1
return
}
const { projectPath, verbose } = options
$.verbose = verbose
// Closing over `verbose` here.
function getProjectSpinner({ text }) {
return verbose
? mockSpinner
: ora({ prefixText: `${chalk.green('[ project ]')}`, text }).start()
}
function getFrameworkSpinner({ text }) {
return verbose
? mockSpinner
: ora({ prefixText: `${chalk.cyan('[framework]')}`, text }).start()
}
const frameworkPath = fileURLToPath(new URL('../../', import.meta.url))
cd(frameworkPath)
performance.mark('startFramework')
const spinner = getFrameworkSpinner({ text: 'building and packing packages' })
await $`yarn nx run-many -t build:pack`
spinner.text = 'moving tarballs'
const tarballDestDirname = 'tarballs'
const tarballDest = path.join(projectPath, tarballDestDirname)
await fs.ensureDir(tarballDest)
const tarballs = await glob(['./packages/**/*.tgz'])
await Promise.all(
tarballs.map((tarball) =>
fs.move(tarball, path.join(tarballDest, path.basename(tarball)), {
overwrite: true,
})
)
)
spinner.text = 'updating resolutions'
const resolutions = (await $`yarn workspaces list --json`).stdout
.trim()
.split('\n')
.map(JSON.parse)
// Filter out the root workspace.
.filter(({ name }) => name)
.reduce((resolutions, { name }) => {
return {
...resolutions,
// Turn a Redwood package name like `@redwoodjs/project-config` into `redwoodjs-project-config.tgz`.
[name]: `./${tarballDestDirname}/${
name.replace('@', '').replaceAll('/', '-') + '.tgz'
}`,
}
}, {})
const projectPackageJsonPath = path.join(projectPath, 'package.json')
const projectPackageJson = await fs.readJSON(projectPackageJsonPath)
await fs.writeJSON(
projectPackageJsonPath,
{
...projectPackageJson,
resolutions: {
...projectPackageJson.resolutions,
...resolutions,
},
},
{
spaces: 2,
}
)
performance.mark('endFramework')
performance.measure('framework', 'startFramework', 'endFramework')
const [entry] = performance.getEntriesByName('framework')
spinner.succeed(`finished in ${(entry.duration / 1000).toFixed(2)} seconds`)
await within(async () => {
cd(projectPath)
performance.mark('startProject')
const spinner = getProjectSpinner({ text: 'yarn install' })
await $`yarn install`
performance.mark('endProject')
performance.measure('project', 'startProject', 'endProject')
const [entry] = performance.getEntriesByName('project')
spinner.succeed(`finished in ${(entry.duration / 1000).toFixed(2)} seconds`)
})
const entries = performance.getEntriesByType('measure').map((entry) => {
return `• ${entry.name} => ${(entry.duration / 1000).toFixed(2)} seconds`
})
for (const entry of entries) {
verbose && console.log(entry)
}
}
main()
async function parseArgs() {
const { positionals, values } = nodeUtilParseArgs({
allowPositionals: true,
options: {
verbose: {
type: 'boolean',
default: false,
short: 'v',
},
},
})
const [projectPath] = positionals
const options = {
verbose: values.verbose,
}
options.projectPath = projectPath ? projectPath : process.env.RWJS_CWD
if (!options.projectPath) {
throw new Error(
[
'Error: You have to provide the path to a Redwood project as',
'',
' 1. the first positional argument',
'',
chalk.gray(' yarn project:tarsync /path/to/redwood/project'),
'',
' 2. the `RWJS_CWD` env var',
'',
chalk.gray(' RWJS_CWD=/path/to/redwood/project yarn project:tarsync'),
].join('\n')
)
}
// This makes `projectPath` an absolute path and throws if it doesn't exist.
options.projectPath = await fs.realpath(options.projectPath)
return options
}