Skip to content

Commit

Permalink
feat(install): support install repo from git
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis committed May 15, 2020
1 parent bc80050 commit 80960cd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
2 changes: 2 additions & 0 deletions packages/feflow-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/command-line-usage": "^5.0.1",
"@types/cross-spawn": "^6.0.0",
"@types/easy-table": "0.0.32",
"@types/execa": "^2.0.0",
"@types/figlet": "^1.2.0",
"@types/inquirer": "^6.0.3",
"@types/js-yaml": "^3.12.1",
Expand Down Expand Up @@ -60,6 +61,7 @@
"commander": "^2.20.0",
"cross-spawn": "^6.0.5",
"easy-table": "^1.1.1",
"execa": "^4.0.1",
"figlet": "^1.2.3",
"import-fresh": "^3.1.0",
"inquire": "^0.4.8",
Expand Down
84 changes: 64 additions & 20 deletions packages/feflow-cli/src/core/native/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,80 @@ import {
getRegistryUrl,
install
} from '../../shared/npm';
import execa from 'execa';
import fs from 'fs';
import path from 'path';
import {
UNIVERSAL_MODULES,
UNIVERSAL_PKG_JSON
} from '../../shared/constant';
import packageJson from '../../shared/packageJson';

async function download(url: string, filepath: string): Promise<any> {
return execa('git', ['clone', url, filepath], {
stdio: 'inherit'
});
}

async function writeDependencies(plugin: string, universalPkgJsonPath: string) {
if (!fs.existsSync(universalPkgJsonPath)) {
fs.writeFileSync(universalPkgJsonPath, JSON.stringify({
'name': 'universal-home',
'version': '0.0.0',
'dependencies': {}
}, null, 2));
}

const universalPkgJson = require(universalPkgJsonPath);
universalPkgJson.dependencies[plugin] = '0.0.0';
fs.writeFileSync(universalPkgJsonPath, JSON.stringify(universalPkgJson, null, 4));
}

module.exports = (ctx: any) => {
const packageManager = ctx.config && ctx.config.packageManager;
const universalModules = path.join(ctx.root, UNIVERSAL_MODULES);
const universalPkgJsonPath = path.join(ctx.root, UNIVERSAL_PKG_JSON);

ctx.commander.register('install', 'Install a devkit or plugin', async () => {
const registryUrl = await getRegistryUrl(packageManager);
const dependencies = ctx.args['_'];

await Promise.all(
dependencies.map((dependency: string) => {
return packageJson(dependency, registryUrl)
.catch(() => {
ctx.logger.error(`${ dependency } not found on ${ packageManager }`);
process.exit(2);
});
})
);
if (/(.git)/.test(dependencies[0])) {
const repoUrl = dependencies[0];
const match = repoUrl.match(/\/([a-zA-Z0-9]*).git$/);
const repoName = match && match[1];
ctx.logger.debug(`Repo name is: ${ repoName }`);
const repoPath = path.join(universalModules, repoName);
if (!fs.existsSync(repoPath)) {
ctx.logger.info(`Start download from ${ repoUrl }`);
await download(repoUrl, repoPath);
}
ctx.logger.debug('Write package to universal-package.json');
await writeDependencies(repoName, universalPkgJsonPath);
} else {
await Promise.all(
dependencies.map((dependency: string) => {
return packageJson(dependency, registryUrl)
.catch(() => {
ctx.logger.error(`${ dependency } not found on ${ packageManager }`);
process.exit(2);
});
})
);

ctx.logger.info('Installing packages. This might take a couple of minutes.');
ctx.logger.info('Installing packages. This might take a couple of minutes.');

return install(
packageManager,
ctx.root,
packageManager === 'yarn' ? 'add' : 'install',
dependencies,
false,
true
).then(() => {
ctx.logger.info('install success');
});
return install(
packageManager,
ctx.root,
packageManager === 'yarn' ? 'add' : 'install',
dependencies,
false,
true
).then(() => {
ctx.logger.info('install success');
});
}
});

ctx.commander.register('uninstall', 'Uninstall a devkit or plugin', () => {
Expand Down

0 comments on commit 80960cd

Please sign in to comment.