Skip to content

Commit

Permalink
feat(universal-package): support download latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis committed May 18, 2020
1 parent d151bcf commit c70dd66
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/feflow-cli/src/core/native/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import execa from 'execa';
import fs from 'fs';
import path from 'path';
import packageJson from '../../shared/packageJson';
import {
getLtsTag,
checkoutVersion
} from '../../shared/npm';
import { parseYaml } from '../../shared/yaml';
import {
UNIVERSAL_MODULES,
Expand All @@ -18,7 +22,7 @@ function download(url: string, filepath: string): Promise<any> {
});
}

function writeDependencies(plugin: string, universalPkgJsonPath: string) {
function writeDependencies(plugin: string, ltsTag: string, universalPkgJsonPath: string) {
if (!fs.existsSync(universalPkgJsonPath)) {
fs.writeFileSync(universalPkgJsonPath, JSON.stringify({
'name': 'universal-home',
Expand All @@ -28,7 +32,7 @@ function writeDependencies(plugin: string, universalPkgJsonPath: string) {
}

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

Expand Down Expand Up @@ -58,22 +62,26 @@ module.exports = (ctx: any) => {

if (/(.git)/.test(dependencies[0])) {
const repoUrl = dependencies[0];
const match = repoUrl.match(/\/(.*).git$/);
const ltsTag = await getLtsTag(repoUrl);
ctx.logger.debug('Latest tag version:', ltsTag);
const match = repoUrl.match(/\/([a-zA-Z0-9]*).git$/);

This comment has been minimized.

Copy link
@blurooo

blurooo May 19, 2020

Collaborator

git地址允许包含 - _ 等字符,要么补充,要么干脆改为 .*

This comment has been minimized.

Copy link
@cpselvis

cpselvis May 19, 2020

Author Collaborator

我加上 -_ 字符

const command = match && match[1];
let repoName: string;
ctx.logger.debug(`Repo name is: ${ command }`);
if (/^feflow-plugin/.test(command)) {
repoName = command;
repoName = `${ command }`;
} else {
repoName = `feflow-plugin-${ command }`;
}
const repoPath = path.join(universalModules, repoName);
const repoPath = path.join(universalModules, `${repoName}@${ltsTag}`);
if (!fs.existsSync(repoPath)) {
ctx.logger.info(`Start download from ${ repoUrl }`);
await download(repoUrl, repoPath);
}
ctx.logger.info(`Switch to version ${ ltsTag}`);
await checkoutVersion(repoPath, ltsTag);
}
ctx.logger.debug('Write package to universal-package.json');

const plugin = resolvePlugin(ctx, repoPath);
// check the validity of the plugin before installing it
await plugin.check();
Expand All @@ -82,7 +90,7 @@ module.exports = (ctx: any) => {
plugin.preInstall.run();
new Linker().register(ctx.bin, ctx.lib, command);

writeDependencies(repoName, universalPkgJsonPath);
writeDependencies(repoName, ltsTag, universalPkgJsonPath);
plugin.test.run();
plugin.postInstall.run([], (out: string, err?: any): boolean => {
out && console.log(out);
Expand Down
38 changes: 38 additions & 0 deletions packages/feflow-cli/src/shared/npm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import spawn from 'cross-spawn';
import childProcess from 'child_process';
import { promisify } from 'util';

export function getRegistryUrl(packageManager: string) {
return new Promise<any>((resolve, reject) => {
Expand Down Expand Up @@ -60,4 +62,40 @@ export function install(packageManager: string, root: any, cmd: any, dependencie
resolve();
});
});
}

export async function getLtsTag(repoUrl: string) {
const execFile = promisify(childProcess.execFile);
const { stdout } = await execFile('git', ['ls-remote', '--tags', repoUrl]);
const tags = new Map();

for (const line of stdout.trim().split('\n')) {
const [hash, tagReference] = line.split('\t');
const tagName = tagReference.replace(/^refs\/tags\//, '').replace(/\^\{\}$/, '');

tags.set(tagName, hash);
}
return [...tags][tags.size - 1][0];
}

export async function checkoutVersion(repoPath: string, version: string) {
return new Promise((resolve, reject) => {
const command = 'git';
const args = [
'-C',
repoPath,
'checkout',
version
];
const child = spawn(command, args, { stdio: 'ignore'});
child.on('close', code => {
if (code !== 0) {
reject({
command: `${command} ${args.join(' ')}`,
});
return;
}
resolve();
});
});
}

0 comments on commit c70dd66

Please sign in to comment.