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

Commit

Permalink
feat: add pnpm support
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Dec 1, 2018
1 parent 743ddb2 commit 7017190
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 545 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
## Quick Start

```bash
yarn global add sao
npm i -g sao

# An official generator for creating a Node.js project
# Generate from git repo
Expand Down
5 changes: 3 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ cli
})
.option(
'--npm-client <client>',
`Use a specific npm client ('yarn' or 'npm')`
`Use a specific npm client ('yarn' | 'npm' | 'pnpm')`
)
.option('-u, --update', 'Update cached generator')
.option('-c, --clone', 'Clone repository instead of archive download')
.option('-y, --yes', 'Use the default options')
.option('--registry <registry>', 'Use a custom registry for npm and yarn')
.option('--registry <registry>', 'Use a custom registry for package manager')
.option('--debug', 'Show debug logs')

cli
.command('set-alias <name> <value>', 'Set an alias for a generator path')
Expand Down
2 changes: 1 addition & 1 deletion lib/GeneratorContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = class GeneratorContext {
}

get npmClient() {
return this.sao.opts.npmClient
return require('./installPackages').getNpmClient()
}

gitInit() {
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class SAO {
typeof this.opts.logLevel === 'number'
? this.opts.logLevel
: this.opts.debug
? 4
: this.opts.quiet
? 1
: 3
? 4
: this.opts.quiet
? 1
: 3
})

this.parsedGenerator = parseGenerator(this.opts.generator)
Expand Down
69 changes: 36 additions & 33 deletions lib/installPackages.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
const readline = require('readline')
const stripAnsi = require('strip-ansi')
const spawn = require('cross-spawn')
const wcwidth = require('wcwidth')
const logUpdate = require('log-update')
const spinner = require('./spinner')
const logger = require('./logger')
const SAOError = require('./SAOError')

let cachedNpmClient = null

function setNpmClient(npmClient) {
if (npmClient) {
cachedNpmClient = npmClient
return npmClient
}

const { stdout, status } = spawn.sync('yarn', ['--version'])
cachedNpmClient = status === 0 && stdout ? 'yarn' : 'npm'
return cachedNpmClient
cachedNpmClient = npmClient
}

function getNpmClient() {
return cachedNpmClient || setNpmClient()
if (cachedNpmClient) {
return cachedNpmClient
}

if (spawn.sync('pnpm', ['--version']).status === 0) {
cachedNpmClient = 'pnpm'
} else if (spawn.sync('yarn', ['--version']).status === 0) {
cachedNpmClient = 'yarn'
}

return cachedNpmClient
}

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

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

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

let output = ''
const stream = process.stderr
let stdoutLogs = ''
let stderrLogs = ''

ps.stdout &&
ps.stdout.on('data', data => {
output += data
ps.stdout.setEncoding('utf8').on('data', data => {
if (npmClient === 'pnpm') {
stdoutLogs = data
} else {
stdoutLogs += data
}
spinner.stop()
stream.write(data)
logUpdate(stdoutLogs)
spinner.start()
})

ps.stderr &&
ps.stderr.on('data', data => {
output += data
ps.stderr.setEncoding('utf8').on('data', data => {
if (npmClient === 'pnpm') {
stderrLogs = data
} else {
stderrLogs += data
}
spinner.stop()
stream.write(data)
logUpdate.clear()
logUpdate.stderr(stderrLogs)
logUpdate(stdoutLogs)
spinner.start()
})

ps.on('close', code => {
spinner.stop()
// Clear output when succeeded
if (code === 0) {
const columns = stream.columns || 80
const lineCount = stripAnsi(output)
.split('\n')
.reduce((count, line) => {
return count + Math.max(1, Math.ceil(wcwidth(line) / columns))
}, 0)
for (let i = 0; i < lineCount; i++) {
if (i > 0) {
readline.moveCursor(stream, 0, -1)
}
readline.clearLine(stream, 0)
readline.cursorTo(stream, 0)
}
logUpdate.clear()
logUpdate.stderr.clear()
logger.success(`Installed ${packageName}`)
} else {
throw new SAOError(`Failed to install ${packageName} in ${cwd}`)
Expand Down
7 changes: 5 additions & 2 deletions lib/updateCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ function performSelfUpdateCheck() {
notifier.update.latest
}", while you're on "${notifier.update.current}".`
)
const isYarn = yarnGlobal.hasDependency('sao')
const isPnpm = __dirname.includes('/pnpm-global/')
const isYarn = !isPnpm && yarnGlobal.hasDependency('sao')
logger.tip(
`To upgrade SAO, run the following command:\n${chalk.dim(
isYarn ? '$ yarn global add sao' : '$ npm i -g sao'
isYarn
? '$ yarn global add sao'
: `$ ${isPnpm ? 'pnpm' : 'npm'} i -g sao`
)}`
)
})
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@
"joycon": "^2.1.2",
"jstransformer": "^1.0.0",
"jstransformer-ejs": "^0.2.0",
"log-update": "^2.3.0",
"majo": "^0.6.2",
"micromatch": "^3.1.10",
"ora": "^3.0.0",
"parse-package-name": "^0.1.0",
"resolve-from": "^4.0.0",
"strip-ansi": "^5.0.0",
"update-notifier": "^2.5.0",
"wcwidth": "^1.0.1",
"yarn-global": "^1.1.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 7017190

Please sign in to comment.