-
Notifications
You must be signed in to change notification settings - Fork 0
feat: auto-install sdlc as dev dependency #5
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,3 @@ | ||
| import { spawn, SpawnOptionsWithoutStdio } from 'child_process'; | ||
| import { syscall } from '../syscall'; | ||
|
|
||
| export async function git( | ||
| command: string, | ||
| args: string[] = [], | ||
| options: SpawnOptionsWithoutStdio = {} | ||
| ): Promise<string> { | ||
| return new Promise((resolve, reject) => { | ||
| const git = spawn('git', [command, ...args], options); | ||
|
|
||
| let stdout = ''; | ||
| let stderr = ''; | ||
|
|
||
| git.stdout.on('data', (data: Buffer) => { | ||
| stdout += data.toString(); | ||
| }); | ||
|
|
||
| git.stderr.on('data', (data: Buffer) => { | ||
| stderr += data.toString(); | ||
| }); | ||
|
|
||
| git.on('close', (code: number) => { | ||
| if (code === 0) { | ||
| resolve(stdout.trim()); | ||
| } | ||
| else { | ||
| reject( | ||
| new Error(`Git command failed with code ${code}: ${stderr}`) | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| git.on('error', (error: Error) => { | ||
| reject(error); | ||
| }); | ||
| }); | ||
| } | ||
| export const git = syscall('git'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { npmInit } from './npm-init'; | ||
| export { npmInstall } from './npm-install'; | ||
| export { npm } from './npm'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { npm } from './npm'; | ||
|
|
||
| export async function npmInit(): Promise<void> { | ||
| await npm('init', ['-y']); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { npm } from './npm'; | ||
|
|
||
| export async function npmInstall(options: { | ||
| package: string; | ||
| isDev?: boolean; | ||
| }): Promise<void> { | ||
| const args = [options.package]; | ||
|
|
||
| if (options.isDev) { | ||
| args.push('--save-dev'); | ||
| } | ||
|
|
||
| await npm('install', args); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { syscall } from '../syscall'; | ||
|
|
||
| export const npm = syscall('npm'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { execSync } from 'child_process'; | ||
|
|
||
| export function syscall(command: string) { | ||
| return async function ( | ||
| subcommand: string, | ||
| args: string[] = [], | ||
| options: { cwd?: string } = {} | ||
| ): Promise<string> { | ||
| try { | ||
| const cwd = options.cwd || process.cwd(); | ||
| args = args.map((arg) => `"${arg.replace(/"/g, '\\"')}"`); | ||
|
||
| const cmd = [command, subcommand, ...args].join(' '); | ||
|
Comment on lines
+11
to
+12
|
||
| const stdout = execSync(cmd, { | ||
| cwd, | ||
| encoding: 'utf8', | ||
| stdio: ['pipe', 'pipe', 'pipe'], | ||
| }); | ||
| return stdout.trim(); | ||
| } | ||
| catch (error) { | ||
| const exitCode = | ||
| error && typeof error === 'object' && 'status' in error | ||
| ? (error as { status?: number }).status | ||
| : undefined; | ||
| const stderr = | ||
| error && | ||
| typeof error === 'object' && | ||
| 'stderr' in error && | ||
| error.stderr | ||
| ? String((error as { stderr?: Buffer }).stderr) | ||
| : (error as Error).message; | ||
| const errMsg = | ||
| `${command} ${subcommand} failed` + | ||
| (exitCode !== undefined ? ` (exit code: ${exitCode})` : '') + | ||
| `: ${stderr}`; | ||
| throw new Error(errMsg); | ||
| } | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import 'jasmine'; | ||
| import { TestUtils } from '../utilities'; | ||
|
|
||
| jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000; | ||
|
|
||
| beforeAll(async () => { | ||
| await TestUtils.cleanupTestDirectories(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { npmInit } from '../../../src/npm'; | ||
| import { TestUtils } from '../../utilities'; | ||
|
|
||
| describe('npm init', () => { | ||
| it('can initialize an npm package', async () => { | ||
| await TestUtils.useDirectory('npm-init-basic'); | ||
| await npmInit(); | ||
|
|
||
| await TestUtils.assertExists('package.json'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { npmInit, npmInstall } from '../../../src/npm'; | ||
| import { TestUtils } from '../../utilities'; | ||
|
|
||
| describe('npm install', () => { | ||
| it('can install a package', async () => { | ||
| await TestUtils.useDirectory('npm-install-basic'); | ||
| await npmInit(); | ||
| await npmInstall({ package: 'ts-tiny-log' }); | ||
|
|
||
| await TestUtils.assertExists('node_modules/ts-tiny-log'); | ||
| }); | ||
|
|
||
| it('can install dev dependencies', async () => { | ||
| await TestUtils.useDirectory('npm-install-dev'); | ||
| await npmInit(); | ||
| await npmInstall({ package: 'ts-tiny-log', isDev: true }); | ||
|
|
||
| await TestUtils.assertExists('node_modules/ts-tiny-log'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { TestUtils } from '../../utilities/test-utilities'; | ||
| import { npm } from '../../../src/npm'; | ||
|
|
||
| describe('npm', () => { | ||
| let testDir: string; | ||
|
|
||
| it('should return stdout when npm command succeeds', async () => { | ||
| testDir = await TestUtils.useDirectory('npm-status'); | ||
| const result = await npm('info', ['ts-tiny-log'], { cwd: testDir }); | ||
|
|
||
| expect(typeof result).toBe('string'); | ||
| expect(result).toBeDefined(); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version
^1.0.0is hardcoded in the init command. This creates a maintenance burden as the version needs to be manually updated when new versions are released. Consider dynamically reading the version from the current package'spackage.jsonor using a constant that can be updated in a single location.