Skip to content

Commit

Permalink
Merge pull request #1372 from opencomponents/npm-install-on-init
Browse files Browse the repository at this point in the history
npm install after oc init
  • Loading branch information
ricardo-devis-agullo authored Aug 3, 2024
2 parents 602afbb + 8e63886 commit 107e362
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/cli/domain/init-template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export default async function initTemplate(options: {
await npm.init(npmOptions);
await installTemplate(options);
await scaffold(Object.assign(options, { compilerPath }));
await npm.installDependencies({
installPath: componentPath,
silent: true,
usePrefix: true
});

return { ok: true };
}
19 changes: 13 additions & 6 deletions src/utils/npm-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import stripVersion from './strip-version';

const buildInstallCommand = (options: {
installPath: string;
all?: boolean;
save?: boolean;
isDev?: boolean;
usePrefix: boolean;
Expand All @@ -14,6 +15,8 @@ const buildInstallCommand = (options: {
args.push('--prefix', options.installPath);
}

if (options.all) return args;

if (options.save) {
args.push('--save-exact');
args.push(options.isDev ? '--save-dev' : '--save');
Expand Down Expand Up @@ -66,22 +69,26 @@ export const init = (options: {
};

export const installDependencies = async (options: {
dependencies: string[];
dependencies?: string[];
installPath: string;
silent: boolean;
usePrefix: boolean;
}): Promise<{ dest: string[] }> => {
const { dependencies, installPath, silent } = options;
const npmi = buildInstallCommand(options);
const npmi = buildInstallCommand({
...options,
all: !dependencies?.length
});
const cmdOptions = {
command: [...npmi, ...dependencies],
command: [...npmi, ...(dependencies ?? [])],
path: installPath,
silent
};

const dest = dependencies.map((dependency) =>
getFullPath({ installPath, dependency })
);
const dest =
dependencies?.map((dependency) =>
getFullPath({ installPath, dependency })
) ?? [];

await executeCommand(cmdOptions);

Expand Down
11 changes: 10 additions & 1 deletion test/unit/cli-domain-init-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('cli : domain : init-template', () => {
'node:path': { join: (...args) => args.join('/') },
'./install-template': stubs.installTemplateStub,
'../../../utils/npm-utils': {
init: stubs.npmStub
init: stubs.npmStub,
installDependencies: stubs.npmStub
},
'./scaffold': stubs.scaffoldStub
}
Expand Down Expand Up @@ -83,6 +84,14 @@ describe('cli : domain : init-template', () => {
expect(o.componentPath).to.equal('path/to/new-component');
expect(o.templateType).to.equal('oc-template-react');
});

it('should call npm install with correct parameters', () => {
expect(npmStub.args[1][0]).to.deep.equal({
installPath: 'path/to/new-component',
silent: true,
usePrefix: true
});
});
});

describe('when component folder creation fails', () => {
Expand Down

0 comments on commit 107e362

Please sign in to comment.