Skip to content

Commit 528717e

Browse files
committed
feat(create): download yarn.js from @yarnpkg/cli-dist
1 parent 24441f8 commit 528717e

File tree

5 files changed

+61
-15
lines changed

5 files changed

+61
-15
lines changed

packages/create/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "create-cordis",
33
"description": "Setup a Cordis application",
4-
"version": "0.1.4",
4+
"version": "0.2.1",
55
"type": "module",
66
"main": "lib/index.js",
77
"types": "lib/index.d.ts",
@@ -35,7 +35,9 @@
3535
"@types/yargs-parser": "^21.0.3"
3636
},
3737
"dependencies": {
38+
"env-paths": "^3.0.0",
3839
"get-registry": "^1.1.0",
40+
"js-yaml": "^4.1.0",
3941
"kleur": "^4.1.5",
4042
"prompts": "^2.4.2",
4143
"tar": "^6.2.1",

packages/create/src/index.ts

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import { mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises'
1+
import { access, copyFile, mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises'
22
import { execSync } from 'node:child_process'
33
import { basename, join, relative } from 'node:path'
44
import { Readable } from 'node:stream'
5-
import { extract } from 'tar'
5+
import * as tar from 'tar'
6+
import * as yaml from 'js-yaml'
7+
import envPaths from 'env-paths'
68
import getRegistry from 'get-registry'
79
import parse from 'yargs-parser'
810
import prompts from 'prompts'
911
import which from 'which-pm-runs'
1012
import kleur from 'kleur'
1113

14+
const paths = envPaths('create-cordis', { suffix: '' })
15+
1216
let project: string
1317
let rootDir: string
1418

@@ -45,6 +49,8 @@ async function confirm(message: string) {
4549
}
4650

4751
class Scaffold {
52+
registry?: string
53+
4854
constructor(public options: Record<string, any> = {}) {}
4955

5056
async getName() {
@@ -81,13 +87,49 @@ class Scaffold {
8187
await mkdir(rootDir)
8288
}
8389

90+
async downloadYarn() {
91+
interface YarnRC {
92+
yarnPath?: string
93+
}
94+
95+
const rc = yaml.load(await readFile(join(rootDir, '.yarnrc.yml'), 'utf8')) as any as YarnRC
96+
const version = rc.yarnPath?.match(/^\.yarn\/releases\/yarn-([^/]+).cjs$/)?.[1]
97+
if (!version) return
98+
99+
const cacheDir = join(paths.cache, '.yarn/releases')
100+
const cacheFile = join(cacheDir, `yarn-${version}.cjs`)
101+
try {
102+
await access(join(cacheDir, `yarn-${version}.cjs`))
103+
} catch {
104+
const tempDir = join(paths.temp, '@yarnpkg/cli-dist')
105+
await mkdir(tempDir, { recursive: true })
106+
await mkdir(cacheDir, { recursive: true })
107+
const resp3 = await fetch(`${this.registry}/@yarnpkg/cli-dist/-/cli-dist-${version}.tgz`)
108+
await new Promise<void>((resolve, reject) => {
109+
const stream = Readable.fromWeb(resp3.body as any).pipe(tar.extract({
110+
cwd: tempDir,
111+
newer: true,
112+
strip: 2,
113+
}, ['package/bin/yarn.js']))
114+
stream.on('finish', resolve)
115+
stream.on('error', reject)
116+
})
117+
await rename(join(tempDir, 'yarn.js'), cacheFile)
118+
}
119+
120+
const targetDir = join(rootDir, '.yarn/releases')
121+
const targetFile = join(targetDir, `yarn-${version}.cjs`)
122+
await mkdir(targetDir, { recursive: true })
123+
await copyFile(targetFile, cacheFile)
124+
}
125+
84126
async scaffold() {
85127
console.log(kleur.dim(' Scaffolding project in ') + project + kleur.dim(' ...'))
86128

87-
const registry = (await getRegistry()).replace(/\/$/, '')
129+
this.registry = (await getRegistry()).replace(/\/$/, '')
88130
const template = argv.template || this.options.template
89131

90-
const resp1 = await fetch(`${registry}/${template}`)
132+
const resp1 = await fetch(`${this.registry}/${template}`)
91133
if (!resp1.ok) {
92134
const { status, statusText } = resp1
93135
console.log(`${kleur.red('error')} request failed with status code ${status} ${statusText}`)
@@ -98,21 +140,26 @@ class Scaffold {
98140

99141
const resp2 = await fetch(remote.versions[version].dist.tarball)
100142
await new Promise<void>((resolve, reject) => {
101-
const stream = Readable.fromWeb(resp2.body as any).pipe(extract({ cwd: rootDir, newer: true, strip: 1 }))
143+
const stream = Readable.fromWeb(resp2.body as any).pipe(tar.extract({
144+
cwd: rootDir,
145+
newer: true,
146+
strip: 1,
147+
}))
102148
stream.on('finish', resolve)
103149
stream.on('error', reject)
104150
})
105151

106-
await this.writePackageJson()
152+
await Promise.all([
153+
this.downloadYarn(),
154+
this.writePackageJson(),
155+
])
107156
console.log(kleur.green(' Done.\n'))
108157
}
109158

110159
async writePackageJson() {
111160
const filename = join(rootDir, 'package.json')
112-
const meta = JSON.parse(await readFile(filename, 'utf-8'))
161+
const meta = JSON.parse(await readFile(filename, 'utf8'))
113162
meta.name = project
114-
meta.private = true
115-
meta.version = '0.0.0'
116163
if (argv.prod) {
117164
// https://github.com/koishijs/koishi/issues/994
118165
// Do not use `NODE_ENV` or `--production` flag.

packages/hmr/.npmignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/hmr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@cordisjs/plugin-hmr",
33
"description": "Hot Module Replacement Plugin for Cordis",
4-
"version": "0.2.2",
4+
"version": "0.2.3",
55
"type": "module",
66
"main": "lib/index.js",
77
"typings": "lib/index.d.ts",

packages/hmr/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ class Watcher extends Service {
9191
})
9292

9393
// files independent from any plugins will trigger a full reload
94-
const parentURL = pathToFileURL(process.cwd()).href
95-
const mainJob = await loader.internal!.getModuleJob('cordis/worker', parentURL, {})!
94+
const mainJob = await loader.internal!.getModuleJob('cordis/worker', import.meta.url, {})!
9695
this.externals = await loadDependencies(mainJob)
9796
const triggerLocalReload = this.ctx.debounce(() => this.triggerLocalReload(), this.config.debounce)
9897

0 commit comments

Comments
 (0)