Skip to content
This repository was archived by the owner on Feb 5, 2022. It is now read-only.

Commit 7017190

Browse files
committed
feat: add pnpm support
1 parent 743ddb2 commit 7017190

File tree

8 files changed

+578
-545
lines changed

8 files changed

+578
-545
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
## Quick Start
2424

2525
```bash
26-
yarn global add sao
26+
npm i -g sao
2727

2828
# An official generator for creating a Node.js project
2929
# Generate from git repo

bin/cli.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ cli
2424
})
2525
.option(
2626
'--npm-client <client>',
27-
`Use a specific npm client ('yarn' or 'npm')`
27+
`Use a specific npm client ('yarn' | 'npm' | 'pnpm')`
2828
)
2929
.option('-u, --update', 'Update cached generator')
3030
.option('-c, --clone', 'Clone repository instead of archive download')
3131
.option('-y, --yes', 'Use the default options')
32-
.option('--registry <registry>', 'Use a custom registry for npm and yarn')
32+
.option('--registry <registry>', 'Use a custom registry for package manager')
33+
.option('--debug', 'Show debug logs')
3334

3435
cli
3536
.command('set-alias <name> <value>', 'Set an alias for a generator path')

lib/GeneratorContext.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = class GeneratorContext {
4141
}
4242

4343
get npmClient() {
44-
return this.sao.opts.npmClient
44+
return require('./installPackages').getNpmClient()
4545
}
4646

4747
gitInit() {

lib/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class SAO {
3030
typeof this.opts.logLevel === 'number'
3131
? this.opts.logLevel
3232
: this.opts.debug
33-
? 4
34-
: this.opts.quiet
35-
? 1
36-
: 3
33+
? 4
34+
: this.opts.quiet
35+
? 1
36+
: 3
3737
})
3838

3939
this.parsedGenerator = parseGenerator(this.opts.generator)

lib/installPackages.js

+36-33
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
const readline = require('readline')
2-
const stripAnsi = require('strip-ansi')
31
const spawn = require('cross-spawn')
4-
const wcwidth = require('wcwidth')
2+
const logUpdate = require('log-update')
53
const spinner = require('./spinner')
64
const logger = require('./logger')
75
const SAOError = require('./SAOError')
86

97
let cachedNpmClient = null
108

119
function setNpmClient(npmClient) {
12-
if (npmClient) {
13-
cachedNpmClient = npmClient
14-
return npmClient
15-
}
16-
17-
const { stdout, status } = spawn.sync('yarn', ['--version'])
18-
cachedNpmClient = status === 0 && stdout ? 'yarn' : 'npm'
19-
return cachedNpmClient
10+
cachedNpmClient = npmClient
2011
}
2112

2213
function getNpmClient() {
23-
return cachedNpmClient || setNpmClient()
14+
if (cachedNpmClient) {
15+
return cachedNpmClient
16+
}
17+
18+
if (spawn.sync('pnpm', ['--version']).status === 0) {
19+
cachedNpmClient = 'pnpm'
20+
} else if (spawn.sync('yarn', ['--version']).status === 0) {
21+
cachedNpmClient = 'yarn'
22+
}
23+
24+
return cachedNpmClient
2425
}
2526

2627
module.exports = async ({
@@ -35,6 +36,8 @@ module.exports = async ({
3536
const packageName = packages ? packages.join(', ') : 'packages'
3637

3738
return new Promise((resolve, reject) => {
39+
// `npm/pnpm/yarn add <packages>`
40+
// `npm/pnpm/yarn install`
3841
const args = [packages ? 'add' : 'install'].concat(packages ? packages : [])
3942
if (saveDev) {
4043
args.push(npmClient === 'npm' ? '-D' : '--dev')
@@ -48,6 +51,7 @@ module.exports = async ({
4851
}
4952

5053
logger.debug(npmClient, args.join(' '))
54+
logger.debug('install directory', cwd)
5155
spinner.start(`Installing ${packageName} with ${npmClient}`)
5256
const ps = spawn(npmClient, args, {
5357
stdio: [0, 'pipe', 'pipe'],
@@ -64,42 +68,41 @@ module.exports = async ({
6468
)
6569
})
6670

67-
let output = ''
68-
const stream = process.stderr
71+
let stdoutLogs = ''
72+
let stderrLogs = ''
6973

7074
ps.stdout &&
71-
ps.stdout.on('data', data => {
72-
output += data
75+
ps.stdout.setEncoding('utf8').on('data', data => {
76+
if (npmClient === 'pnpm') {
77+
stdoutLogs = data
78+
} else {
79+
stdoutLogs += data
80+
}
7381
spinner.stop()
74-
stream.write(data)
82+
logUpdate(stdoutLogs)
7583
spinner.start()
7684
})
7785

7886
ps.stderr &&
79-
ps.stderr.on('data', data => {
80-
output += data
87+
ps.stderr.setEncoding('utf8').on('data', data => {
88+
if (npmClient === 'pnpm') {
89+
stderrLogs = data
90+
} else {
91+
stderrLogs += data
92+
}
8193
spinner.stop()
82-
stream.write(data)
94+
logUpdate.clear()
95+
logUpdate.stderr(stderrLogs)
96+
logUpdate(stdoutLogs)
8397
spinner.start()
8498
})
8599

86100
ps.on('close', code => {
87101
spinner.stop()
88102
// Clear output when succeeded
89103
if (code === 0) {
90-
const columns = stream.columns || 80
91-
const lineCount = stripAnsi(output)
92-
.split('\n')
93-
.reduce((count, line) => {
94-
return count + Math.max(1, Math.ceil(wcwidth(line) / columns))
95-
}, 0)
96-
for (let i = 0; i < lineCount; i++) {
97-
if (i > 0) {
98-
readline.moveCursor(stream, 0, -1)
99-
}
100-
readline.clearLine(stream, 0)
101-
readline.cursorTo(stream, 0)
102-
}
104+
logUpdate.clear()
105+
logUpdate.stderr.clear()
103106
logger.success(`Installed ${packageName}`)
104107
} else {
105108
throw new SAOError(`Failed to install ${packageName} in ${cwd}`)

lib/updateCheck.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ function performSelfUpdateCheck() {
2323
notifier.update.latest
2424
}", while you're on "${notifier.update.current}".`
2525
)
26-
const isYarn = yarnGlobal.hasDependency('sao')
26+
const isPnpm = __dirname.includes('/pnpm-global/')
27+
const isYarn = !isPnpm && yarnGlobal.hasDependency('sao')
2728
logger.tip(
2829
`To upgrade SAO, run the following command:\n${chalk.dim(
29-
isYarn ? '$ yarn global add sao' : '$ npm i -g sao'
30+
isYarn
31+
? '$ yarn global add sao'
32+
: `$ ${isPnpm ? 'pnpm' : 'npm'} i -g sao`
3033
)}`
3134
)
3235
})

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@
3333
"joycon": "^2.1.2",
3434
"jstransformer": "^1.0.0",
3535
"jstransformer-ejs": "^0.2.0",
36+
"log-update": "^2.3.0",
3637
"majo": "^0.6.2",
3738
"micromatch": "^3.1.10",
3839
"ora": "^3.0.0",
3940
"parse-package-name": "^0.1.0",
4041
"resolve-from": "^4.0.0",
41-
"strip-ansi": "^5.0.0",
4242
"update-notifier": "^2.5.0",
43-
"wcwidth": "^1.0.1",
4443
"yarn-global": "^1.1.0"
4544
},
4645
"devDependencies": {

0 commit comments

Comments
 (0)