diff --git a/src/commands/init.ts b/src/commands/init.ts index f1c26d2..9c7c6f3 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -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' @@ -62,7 +63,9 @@ one. flags['pkg-manager'] ?? findPackageManager() ?? (await askPackageManager()) - installDeps(pkgManager) + await installDeps(pkgManager) + + generateBaseFiles() p.outro('🚀 publicodes is ready to use!') } @@ -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)) @@ -114,8 +124,8 @@ function askPackageJsonInfo(): Promise { 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: () => @@ -166,15 +176,26 @@ function askPackageManager(): Promise { }) as Promise } -function installDeps(pkgManager: PackageManager): void { +async function installDeps(pkgManager: PackageManager): Promise { 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') } diff --git a/src/utils/pjson.ts b/src/utils/pjson.ts index 6045e89..dcff394 100644 --- a/src/utils/pjson.ts +++ b/src/utils/pjson.ts @@ -8,6 +8,7 @@ export type PackageJson = { type?: string types?: string files?: string[] + // TODO: infer from the git config repository?: { url: string type: string @@ -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', @@ -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 { diff --git a/yarn.lock b/yarn.lock index 2c9d851..af81b9c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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/rollup-android-arm-eabi@4.18.0": version "4.18.0" resolved "https://registry.yarnpkg.com/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.18.0.tgz#bbd0e616b2078cd2d68afc9824d1fadb2f2ffd27"