Skip to content

Commit

Permalink
fix: support add npm packages
Browse files Browse the repository at this point in the history
  • Loading branch information
neikvon committed Dec 16, 2020
1 parent 5fc08d0 commit 0ac3a8c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
82 changes: 48 additions & 34 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import type { Factory } from '../core/factory'

import { join, relative } from 'path'
import { Command } from '../core/command'
import { git, isGitUrl, pathResolve, remotePkgVersion } from '../utils'
import { git, isGitUrl, remotePkgVersion } from '../utils'

export default class CommandAdd extends Command {
id = 'add'
alias = ''
args = '<factories...>'
description = `add factories from npm module or git url`
flags = [
['-l, --local', 'Add to local node_modules', false],
['-y, --yes', 'Yes to all questions', false],
['-t, --target-dir <dir>', 'Target dir for factory from npm', ''],
['-p, --package-manager <name>', 'Specifying a package manager. e.g. pnpm/yarn/npm']
]
examples = ['fbi add factory-node', 'fbi add @fbi-js/factory-node -t sub-dir -y']

constructor(public factory: Fbi) {
super()
Expand All @@ -28,8 +30,7 @@ export default class CommandAdd extends Command {

for (const name of names) {
// 1. try npm module
let factory = await this.addFromNpm(name)
console.log({ factory })
let factory = await this.addFromNpm(name, flags)

if (!factory) {
// 2. try git repository
Expand Down Expand Up @@ -83,7 +84,8 @@ export default class CommandAdd extends Command {
return true
}

private async addFromNpm(name: string, cwd = process.cwd()): Promise<null | Factory> {
private async addFromNpm(name: string, flags: any): Promise<null | Factory> {
const cwd = join(process.cwd(), flags?.targetDir ?? '')
const factoryExist = await this.factoryExist(name, 'npm', cwd)
this.debug('factoryExist:', factoryExist)

Expand All @@ -93,32 +95,38 @@ export default class CommandAdd extends Command {
return null
}

const relativePath = relative(process.cwd(), cwd)
if (!flags.yes) {
const relativePath = relative(process.cwd(), cwd)

const anwser = (await this.prompt({
type: 'confirm',
name: 'confirm',
message: `'${name}' will be installed in the ${
relativePath || 'current'
} directory, continue?`,
initial: true
})) as any
const anwser = (await this.prompt({
type: 'confirm',
name: 'confirm',
message: `'${name}' will be installed in ${
relativePath ? `'${relativePath}'` : 'current'
} directory, continue?`,
initial: true
})) as any

if (!anwser.confirm) {
this.exit()
return null
if (!anwser.confirm) {
this.exit()
return null
}
}
}

await this.fs.ensureDir(join(cwd, 'node_modules'))
}
const styledName = this.style.cyan(name)
const spinner = this.createSpinner(
`${factoryExist ? 'Updating' : 'Installing'} ${styledName}`
).start()

try {
await this.exec.command(`npm install --no-package-lock ${name}`, {
const cmd = `npm install --no-package-lock ${name}`
const opts = {
cwd
})
}
this.debug({ cmd, opts })
await this.exec.command(cmd, opts)
spinner.succeed(`${factoryExist ? 'Updated' : 'Installed'} ${styledName}`)
return this.factory.resolveFromLocal(name, cwd)
} catch (err) {
Expand Down Expand Up @@ -196,7 +204,7 @@ export default class CommandAdd extends Command {
}

spinner.succeed(`${isUpdate ? 'Updated' : 'Added'} ${styledName}`)
await this.install(flags || {}, targetDir)
await this.installProdDeps(flags || {}, targetDir)

const factory = this.factory.createFactory(targetDir)
if (!factory) {
Expand Down Expand Up @@ -232,23 +240,29 @@ export default class CommandAdd extends Command {
}

private async factoryExist(name: string, type: 'npm' | 'git', cwd = process.cwd()) {
if (type === 'npm') {
return pathResolve(name, {
paths: [cwd]
})
} else {
const config = this.context.get('config')
const targetDir = join(config?.rootDirectory, config?.directoryName, name)
const exist = await this.fs.pathExists(targetDir)
return exist ? targetDir : ''
}
const config = this.context.get('config')
const targetDir =
type === 'npm'
? join(cwd, 'node_modules', name)
: join(config?.rootDirectory, config?.directoryName, name)
const exist = await this.fs.pathExists(targetDir)
return exist ? targetDir : ''
}

private async install(flags: Record<string, any>, targetDir: string) {
private async installProdDeps(flags: Record<string, any>, targetDir: string) {
const spinner = this.createSpinner(`Installing dependencies...`).start()
try {
await this.installDeps(targetDir, flags.packageManager, false, {
stdout: 'ignore'
process.env.NODE_ENV = 'production'
const env = this.context.get('env')
const pm =
flags.packageManager || (env.hasYarn ? 'yarn' : this.context.get('config').packageManager)
await this.exec.command(`${pm} install`, {
cwd: targetDir,
stdout: 'ignore',
env: {
...process.env,
NODE_ENV: 'production'
}
})
spinner.succeed(`Installed dependencies`)
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export abstract class BaseClass {
cmds.push(pm === 'npm' ? '--no-package-lock' : pm === 'yarn' ? '--no-lockfile' : '')
}

if (npmVersion && npmVersion[0] >= 7) {
if (pm === 'npm' && npmVersion && npmVersion[0] >= 7) {
cmds.push('--legacy-peer-deps')
}

Expand Down
18 changes: 18 additions & 0 deletions src/core/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class Version extends BaseClass {

await git.hardReset(version.long, { cwd: version.dir })
await git.checkout(version.long, { cwd: version.dir })
await this.installProdDeps(version.dir)
}

return version
Expand Down Expand Up @@ -152,4 +153,21 @@ export class Version extends BaseClass {
}
}
}

private async installProdDeps(targetDir: string) {
try {
const env = this.context.get('env')
const pm = env.hasYarn ? 'yarn' : this.context.get('config').packageManager
await this.exec.command(`${pm} install`, {
cwd: targetDir,
stdout: 'ignore',
env: {
...process.env,
NODE_ENV: 'production'
}
})
} catch (err) {
this.error(err)
}
}
}

0 comments on commit 0ac3a8c

Please sign in to comment.