Skip to content
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

npm install after oc init #1372

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading