Skip to content

Commit

Permalink
fix: Preinstall for either npm or yarn.
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
joberstein committed Mar 6, 2023
1 parent 68bc9c4 commit afaf196
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/preinstall.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { execSync } from "child_process";
import TestUtils from "./test/util";
import preinstall from "./preinstall";

const packageName = '@joberstein12/commitlint-config';

describe("src/preinstall", () => {
const {
createTempDirectory,
teardownTestDirectory,
options,
} = new TestUtils();

beforeEach(() => {
options.cwd = createTempDirectory();
process.chdir(options.cwd);
});

afterEach(() => {
teardownTestDirectory(options.cwd as string);
});

it("Installs for an npm app", () => {
execSync('npm init -y && npm install', options);

preinstall(packageName, options);

const isInstalled = execSync(`npm list ${packageName} | grep ${packageName}`)
.toString()
.trim();

expect(isInstalled).toBeTruthy();
});

it("Installs for a yarn app", () => {
execSync('yarn init -y && yarn install', options);

preinstall(packageName, options);

const isInstalled = execSync(`yarn list --pattern ${packageName} | grep ${packageName}`)
.toString()
.trim();

expect(isInstalled).toBeTruthy();
});
});
7 changes: 6 additions & 1 deletion src/preinstall.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExecSyncOptions, execSync } from 'child_process';
import { existsSync } from 'fs';

/**
* Pre-install extra dependencies.
Expand All @@ -15,6 +16,10 @@ export default (packageString?: string, options?: ExecSyncOptions): void => {
.join(' ');

if (packages) {
execSync(`npm install ${packages} --silent`, options);
const command = existsSync('yarn.lock')
? `yarn add ${packages} --silent`
: `npm install ${packages} --silent`;

execSync(command, options);
}
};

0 comments on commit afaf196

Please sign in to comment.