Skip to content

Commit

Permalink
fix(command/init): fix spinning in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Oct 1, 2024
1 parent 16081c3 commit 8b035f0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
45 changes: 33 additions & 12 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import chalk from 'chalk'

import { basePackageJson, getPackageJson, PackageJson } from '../utils/pjson'
import { OptionFlag } from '@oclif/core/lib/interfaces'
import { spawn } from 'child_process'

type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun'

Expand Down Expand Up @@ -62,7 +63,9 @@ one.
flags['pkg-manager'] ??
findPackageManager() ??
(await askPackageManager())
installDeps(pkgManager)
await installDeps(pkgManager)

generateBaseFiles()

p.outro('🚀 publicodes is ready to use!')
}
Expand All @@ -86,6 +89,13 @@ one.
...pkgJSON.devDependencies,
'@publicodes/tools': `^${this.config.pjson.version}`,
}
pkgJSON.scripts = {
...pkgJSON.scripts,
...basePackageJson.scripts,
}
if (pkgJSON.name.startsWith('@')) {
pkgJSON.publishConfig = { access: 'public' }
}

try {
fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJSON, null, 2))
Expand Down Expand Up @@ -114,8 +124,8 @@ function askPackageJsonInfo(): Promise<PackageJson> {
version: () =>
p.text({
message: 'Version',
defaultValue: '1.0.0',
placeholder: '1.0.0',
defaultValue: '0.1.0',
placeholder: '0.1.0',
}),
author: () => p.text({ message: 'Author', defaultValue: '' }),
license: () =>
Expand Down Expand Up @@ -166,15 +176,26 @@ function askPackageManager(): Promise<PackageManager> {
}) as Promise<PackageManager>
}

function installDeps(pkgManager: PackageManager): void {
async function installDeps(pkgManager: PackageManager): Promise<void> {
const s = p.spinner()

s.start(`Installing dependencies with ${pkgManager}`)
try {
execSync(`${pkgManager} install -y`, { stdio: 'ignore' })
s.stop('Dependencies installed with success', 0)
} catch (error) {
p.log.error(error.message)
p.cancel('An error occurred while installing dependencies')
process.exit(1)
}
return new Promise((resolve) => {
const program = spawn(pkgManager, ['install', '-y'], { stdio: 'ignore' })

program.on('error', (error) => {
s.stop('An error occurred while installing dependencies')
p.log.error(error.message)
process.exit(1)
})

program.on('close', () => {
s.stop('Dependencies installed')
resolve()
})
})
}

function generateBaseFiles() {
p.log.step('Generating files')
}
9 changes: 8 additions & 1 deletion src/utils/pjson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type PackageJson = {
type?: string
types?: string
files?: string[]
// TODO: infer from the git config
repository?: {
url: string
type: string
Expand All @@ -26,11 +27,14 @@ export type PackageJson = {
devDependencies?: {
[key: string]: string
}
publishConfig?: {
access: string
}
}

export const basePackageJson: PackageJson = {
name: '',
version: '1.0.0',
version: '0.1.0',
description: '',
author: '',
type: 'module',
Expand All @@ -42,6 +46,9 @@ export const basePackageJson: PackageJson = {
// TODO: how to get the latest version of publicodes?
publicodes: '^1.5.1',
},
scripts: {
build: 'publicodes build',
},
}

export function getPackageJson(): PackageJson | undefined {
Expand Down
11 changes: 0 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,6 @@
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==

"@publicodes/tools@^1.2.5":
version "1.2.5"
resolved "https://registry.yarnpkg.com/@publicodes/tools/-/tools-1.2.5.tgz#ab3177a027d530718057600f78512473a392d8e7"
integrity sha512-Cv2nhqd8ucKhFNi6g9aiUqV+PghAdZXk0ECmzXUQjr7AWYVoQZo+/b6rnGcR5uuwg3HOoYqRY8sUenvz80I2sg==
dependencies:
"@types/node" "^18.11.18"
glob "^10.4.1"
path "^0.12.7"
publicodes "^1.3.3"
yaml "^2.4.5"

"@rollup/[email protected]":
version "4.18.0"
resolved "https://registry.yarnpkg.com/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.18.0.tgz#bbd0e616b2078cd2d68afc9824d1fadb2f2ffd27"
Expand Down

0 comments on commit 8b035f0

Please sign in to comment.