Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Configure.installPackages method #4188

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions commands/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import { EnvEditor } from '../modules/env.js'
import type { ApplicationService } from '../src/types.js'
import { args, BaseCommand } from '../modules/ace/main.js'
import { installPackage, detectPackageManager } from '@antfu/install-pkg'
import { fileURLToPath } from 'node:url'

Check failure on line 15 in commands/configure.ts

View workflow job for this annotation

GitHub Actions / typecheck / typecheck

'fileURLToPath' is declared but its value is never read.

Check failure on line 15 in commands/configure.ts

View workflow job for this annotation

GitHub Actions / typecheck / typecheck

'fileURLToPath' is declared but its value is never read.

/**
* The configure command is used to configure packages after installation
Expand Down Expand Up @@ -111,6 +113,41 @@
this.logger.action('update .adonisrc.json file').succeeded()
}

/**
* Install packages using the correct package manager
* You can specify version of each package by setting it in the
* name like :
*
* ```
* installPackages(['@adonisjs/lucid@next', '@adonisjs/[email protected]'])
* ```
*/
async installPackages(packages: { name: string; isDevDependency: boolean }[]) {
const appPath = this.app.makePath()

const devDeps = packages.filter((pkg) => pkg.isDevDependency).map(({ name }) => name)
const deps = packages.filter((pkg) => !pkg.isDevDependency).map(({ name }) => name)

const packageManager = await detectPackageManager(appPath)
let spinner = this.logger
.await(`installing dependencies using ${packageManager || 'npm'}`)
.start()

try {
await installPackage(deps, { cwd: appPath, silent: true })
await installPackage(devDeps, { dev: true, cwd: appPath, silent: true })

spinner.stop()
this.logger.success('dependencies installed')
this.logger.log(devDeps.map((dep) => ` ${this.colors.dim('dev')} ${dep}`).join('\n'))
this.logger.log(deps.map((dep) => ` ${this.colors.dim('prod')} ${dep}`).join('\n'))
} catch (error) {
spinner.update('unable to install dependencies')
spinner.stop()
this.logger.fatal(error)
}
}

/**
* List the packages one should install before using the packages
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"@adonisjs/hash": "^8.3.1-3",
"@adonisjs/http-server": "^6.8.2-7",
"@adonisjs/logger": "^5.4.2-3",
"@antfu/install-pkg": "^0.1.1",
"@adonisjs/repl": "^4.0.0-5",
"@paralleldrive/cuid2": "^2.2.0",
"@poppinss/macroable": "^1.0.0-7",
Expand Down
142 changes: 142 additions & 0 deletions tests/commands/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,146 @@ test.group('Configure command | run', (group) => {
await command.exec()
assert.equal(command.result, 'configured')
})

test('install packages', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => {
return import(new URL(filePath, fs.baseUrl).href)
},
})

await ace.app.init()
ace.ui.switchMode('raw')

await fs.create('pnpm-lock.yaml', '')
await fs.createJson('package.json', { type: 'module' })
await fs.create(
'dummy-pkg.js',
`
export const stubsRoot = './'
export async function configure (command) {
await command.installPackages([
{ name: '[email protected]', isDevDependency: true },
{ name: '[email protected]', isDevDependency: false }
])
}
`
)

const command = await ace.create(Configure, ['./dummy-pkg.js?v=3'])
await command.exec()

const packageJson = await fs.contentsJson('package.json')
assert.deepEqual(packageJson.dependencies, { 'is-even': '1.0.0' })
assert.deepEqual(packageJson.devDependencies, { 'is-odd': '2.0.0' })
}).timeout(5000)

test('install packages and detect pnpm', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => {
return import(new URL(filePath, fs.baseUrl).href)
},
})

await ace.app.init()
ace.ui.switchMode('raw')

await fs.create('pnpm-lock.yaml', '')
await fs.createJson('package.json', { type: 'module' })
await fs.create(
'dummy-pkg.js',
`
export const stubsRoot = './'
export async function configure (command) {
await command.installPackages([
{ name: '[email protected]', isDevDependency: true, },
])
}
`
)

const command = await ace.create(Configure, ['./dummy-pkg.js?v=4'])
await command.exec()

const logs = ace.ui.logger.getLogs()
assert.deepInclude(logs, {
message: '[ cyan(wait) ] installing dependencies using pnpm . ',
stream: 'stdout',
})
}).timeout(5000)

test('install packages and detect npm', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => {
return import(new URL(filePath, fs.baseUrl).href)
},
})

await ace.app.init()
ace.ui.switchMode('raw')

await fs.createJson('package-lock.json', {})
await fs.createJson('package.json', { type: 'module' })
await fs.create(
'dummy-pkg.js',
`
export const stubsRoot = './'
export async function configure (command) {
await command.installPackages([
{ name: '[email protected]', isDevDependency: true, },
])
}
`
)

const command = await ace.create(Configure, ['./dummy-pkg.js?v=5'])
await command.exec()

const logs = ace.ui.logger.getLogs()

assert.deepInclude(logs, {
message: '[ cyan(wait) ] installing dependencies using npm . ',
stream: 'stdout',
})
}).timeout(5000)

test('display error when installing packages', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => {
return import(new URL(filePath, fs.baseUrl).href)
},
})

await ace.app.init()
ace.ui.switchMode('raw')

await fs.createJson('package-lock.json', {})
await fs.createJson('package.json', { type: 'module' })
await fs.create(
'dummy-pkg.js',
`
export const stubsRoot = './'
export async function configure (command) {
await command.installPackages([
{ name: '[email protected]', isDevDependency: true, },
])
}
`
)

const command = await ace.create(Configure, ['./dummy-pkg.js?v=6'])
await command.exec()

const logs = ace.ui.logger.getLogs()
assert.deepInclude(logs, {
message: '[ cyan(wait) ] unable to install dependencies ...',
stream: 'stdout',
})

const lastLog = logs[logs.length - 1]
assert.deepInclude(
lastLog.message,
'[ red(error) ] Command failed with exit code 1: npm install -D [email protected]'
)
}).timeout(5000)
})
Loading