Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: get npm client from storage #2473

Merged
merged 5 commits into from
Jul 17, 2019
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
10 changes: 2 additions & 8 deletions packages/iceworks-server/src/app/io/controller/home/setting.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import * as path from 'path';
import { checkAliInternal } from 'ice-npm-utils';
import storage from '../../../../lib/storage';
import scanDirectory from '../../../../lib/scanDirectory';
import getNpmClient from '../../../../lib/getNpmClient';

export default (app) => {
const { Controller, i18n } = app;
Expand Down Expand Up @@ -70,12 +69,7 @@ export default (app) => {
}

public async getNpmClient() {
let npmClient = storage.get('npmClient');
if (!npmClient) {
npmClient = await checkAliInternal() ? 'tnpm' : 'npm';
storage.set('npmClient', npmClient);
}
return npmClient;
return await getNpmClient();
}

public async setUser({ args }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ import * as util from 'util';
import * as rimraf from 'rimraf';
import * as execa from 'execa';
import * as latestVersion from 'latest-version';
import getNpmClient from '../../../getNpmClient';

import { IDependency, IProject, ICreateDependencyParam, IDependencyModule, ISocket, IContext } from '../../../../interface';

const rimrafAsync = util.promisify(rimraf);

export const install = async (
dependencies: ICreateDependencyParam[], npmClient: string, isDev: boolean, project: IProject, namespace: string, ctx: IContext
params: {
dependencies: ICreateDependencyParam[];
npmClient: string;
isDev: boolean;
project: IProject;
namespace: string;
ctx: IContext;
}
): Promise<void> => {
const { dependencies, npmClient, isDev, project, namespace, ctx } = params;
const { socket, i18n, logger } = ctx;
logger.info('dependencies', dependencies);
socket.emit(`adapter.${namespace}.install.data`, i18n.format('baseAdapter.dependency.reset.startInstall'));
Expand Down Expand Up @@ -77,8 +86,7 @@ export default class Dependency implements IDependencyModule {
let npmOutdated = [];

try {
await execa('npm', ['outdated', '--json'], { cwd: this.project.path, env: this.project.getEnv() });
const npmClient = this.storage.get('npmClient');
const npmClient = await getNpmClient();
await execa(npmClient, ['outdated', '--json'], { cwd: this.project.path, env: this.project.getEnv() });
} catch (error) {
if (error.errno) {
Expand All @@ -92,14 +100,30 @@ export default class Dependency implements IDependencyModule {
return Object.entries(npmOutdated).map(([key, value]: [string, { current: string; wanted: string; latest: string; location: string }]) => ({ package: key, ...value }));
}

public async create(params: {dependency: ICreateDependencyParam; idDev?: boolean}, ctx: IContext): Promise<void> {
const { dependency, idDev } = params;
return (await install([dependency], this.storage.get('npmClient'), idDev, this.project, 'dependency', ctx))[0];
public async create(params: {dependency: ICreateDependencyParam; isDev?: boolean}, ctx: IContext): Promise<void> {
const { dependency, isDev } = params;
const npmClient = await getNpmClient();
return (await install({
dependencies: [dependency],
npmClient,
isDev,
project: this.project,
namespace: 'dependency',
ctx,
}))[0];
}

public async bulkCreate(params: {dependencies: ICreateDependencyParam[]; idDev?: boolean}, ctx: IContext): Promise<void> {
const { dependencies, idDev } = params;
return await install(dependencies, this.storage.get('npmClient'), idDev, this.project, 'dependency', ctx);
public async bulkCreate(params: {dependencies: ICreateDependencyParam[]; isDev?: boolean}, ctx: IContext): Promise<void> {
const { dependencies, isDev } = params;
const npmClient = await getNpmClient();
return await install({
dependencies,
npmClient,
isDev,
project: this.project,
namespace: 'dependency',
ctx,
});
}

public async getAll(): Promise<{ dependencies: IDependency[]; devDependencies: IDependency[] }> {
Expand Down Expand Up @@ -163,7 +187,8 @@ export default class Dependency implements IDependencyModule {

socket.emit('adapter.dependency.reset.data', i18n.format('baseAdapter.dependency.reset.startInstall'));

const childProcess = execa('npm', ['install', '--loglevel', 'silly'], {
const npmClient = await getNpmClient();
const childProcess = execa(npmClient, ['install', '--loglevel', 'silly'], {
cwd: this.project.path,
env: this.project.getEnv(),
stdio: ['inherit', 'pipe', 'pipe'],
Expand Down Expand Up @@ -197,7 +222,8 @@ export default class Dependency implements IDependencyModule {

socket.emit('adapter.dependency.upgrade.data', i18n.format('baseAdapter.dependency.reset.startInstall', {packageName}));

const childProcess = execa('npm', ['update', packageName, '--loglevel', 'silly'], {
const npmClient = await getNpmClient();
const childProcess = execa(npmClient, ['update', packageName, '--loglevel', 'silly'], {
cwd: this.project.path,
env: this.project.getEnv(),
stdio: ['inherit', 'pipe', 'pipe'],
Expand Down
11 changes: 10 additions & 1 deletion packages/iceworks-server/src/lib/adapter/modules/page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as upperCamelCase from 'uppercamelcase';
import * as kebabCase from 'kebab-case';
import { getAndExtractTarball } from 'ice-npm-utils';
import scanDirectory from '../../../scanDirectory';
import getNpmClient from '../../../getNpmClient';
import getIceVersion from '../../utils/getIceVersion';
import getTarballURLByMaterielSource from '../../../getTarballURLByMaterielSource';
import { install as installDependency } from '../dependency';
Expand Down Expand Up @@ -95,7 +96,15 @@ export default class Page implements IPageModule {

return await Promise.all(filterDependencies.map(async (dependency) => {
const [packageName, version]: [string, string] = Object.entries(dependency)[0];
return await installDependency([{ package: packageName, version }], false, this.project, 'page', ctx);
const npmClient = await getNpmClient();
return await installDependency({
dependencies: [{ package: packageName, version }],
npmClient,
isDev: false,
project: this.project,
namespace: 'page',
ctx,
});
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as terminate from 'terminate';
import * as os from 'os';
import chalk from 'chalk';
import { getCLIConf, setCLIConf, mergeCLIConf } from '../../utils/cliConf';
import getNpmClient from '../../../getNpmClient';
import { ITaskModule, ITaskParam, IProject, IContext, ITaskConf } from '../../../../interface';
import getTaskConfig from './getTaskConfig';

Expand Down Expand Up @@ -58,7 +59,7 @@ export default class Task implements ITaskModule {
if (command === 'dev') {
env = { PORT: await detectPort(DEFAULT_PORT) };
}
const npmClient = this.storage.get('npmClient');
const npmClient = await getNpmClient();
const eventName = `start.data.${command}`;

try {
Expand Down
14 changes: 14 additions & 0 deletions packages/iceworks-server/src/lib/getNpmClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { checkAliInternal } from 'ice-npm-utils';
import storage from './storage';

const getNpmClient = async () => {
let npmClient = storage.get('npmClient');
if (!npmClient) {
// set default npm client
npmClient = await checkAliInternal() ? 'tnpm' : 'npm';
storage.set('npmClient', npmClient);
}
return npmClient;
};

export default getNpmClient;