Skip to content

Commit

Permalink
feat:✅ 完成代理安装npm
Browse files Browse the repository at this point in the history
  • Loading branch information
KAWATAAAAAA committed Sep 3, 2021
1 parent 212e8a7 commit 0e557a6
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 54 deletions.
31 changes: 31 additions & 0 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = __importDefault(require("../utils/index"));
var Shell = require("shelljs");
var chalk = require("chalk");
var privatePrefix = "@xsyx";
var privateResource = "https://mirrors.xsyxxd.com/repository/npm-group/";
module.exports = function (arg, mode) {
var pkg = require(process.cwd() + "/package.json");
console.log(chalk.green("🌵 正在由HaYa-CLI代理安装 npm 包...."));
index_1.default.each(pkg.dependencies, eachHandler);
index_1.default.each(pkg.devDependencies, eachHandler);
};
function eachHandler(_a) {
var key = _a.key, originObject = _a.originObject;
var name = key;
// const reg = /^(@xsyx)/
var reg = new RegExp("(^" + privatePrefix + "/)", "i");
/* 拿到版本号,并去掉修饰符 */
var version = originObject[name];
/* shell 语法: npm install [<@scope>/]<name>@<version range> */
if (reg.test(name)) {
Shell.exec("npm install " + name + "@\"" + version + "\" --registry " + privateResource);
}
else {
Shell.exec("npm install " + name + "@\"" + version + "\" --registry https://registry.npmjs.org/");
}
}
18 changes: 18 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
// command dispatcher
var Command = require("commander").Command;
var program = new Command();
var chalk = require("chalk");
var createCommand = require("./commands/create");
var installCommand = require("./commands/install");
var serveCommand = require("./commands/serve");
var buildCommand = require("./commands/build");
var mapActions = {
Expand Down Expand Up @@ -44,6 +52,16 @@ program
//require(path.resolve(__dirname, `commands/create`))(...process.argv.slice(3));
createCommand.apply(void 0, process.argv.slice(3));
});
program
.command("install")
.alias("i")
.description("代理npm安装")
.option("-D, --dev", "指定安装到 devDependencies")
.option("-S, --save", "指定安装到 dependencies")
.action(function (option) {
//require(path.resolve(__dirname, `commands/create`))(...process.argv.slice(3));
installCommand.apply(void 0, __spreadArrays(process.argv.slice(3), [option]));
});
program
.command("serve")
.alias("s")
Expand Down
10 changes: 7 additions & 3 deletions lib/plugins/rollup-plugin-write.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use strict";
//import { name as appName, version as appVersion} from "./package.json"
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand Down Expand Up @@ -35,10 +36,13 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
//import { name as appName, version as appVersion} from "./package.json"
var safeRequire = require("../utils/index").safeRequire;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = __importDefault(require("../utils/index"));
var path = require("path");
var pkg = safeRequire("" + path.resolve(process.cwd(), "package.json"));
var pkg = index_1.default.safeRequire("" + path.resolve(process.cwd(), "package.json"));
var appName = pkg ? pkg.name : "demo";
var appVersion = pkg ? pkg.version : "0.0.0";
var fs = require("fs-extra");
Expand Down
17 changes: 16 additions & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ var safeRequire = function (path) {
return null;
}
};
module.exports = {
function each(obj, iteratorFn) {
Reflect.ownKeys(obj).forEach(function (key, index, array) {
iteratorFn({
key: key,
index: index,
array: array,
originObject: obj,
});
});
}
// module.exports = {
// safeRequire,
// each
// };
exports.default = {
safeRequire: safeRequire,
each: each,
};
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@types/metalsmith": "^2.3.0",
"@vitejs/plugin-legacy": "^1.3.1",
"@vitejs/plugin-vue": "^1.1.4",
"@xsyx/brick-filter": "^0.2.34",
"axios": "^0.21.1",
"commander": "^7.1.0",
"consolidate": "^0.16.0",
Expand All @@ -49,8 +50,10 @@
"fs-extra": "^9.1.0",
"ini": "^2.0.0",
"inquirer": "^7.3.3",
"lodash-es": "^4.17.21",
"metalsmith": "^2.3.0",
"ora": "^5.3.0",
"shelljs": "^0.8.4",
"util": "^0.12.3",
"vite": "^2.4.4",
"vite-plugin-mock": "^2.1.4"
Expand Down
42 changes: 42 additions & 0 deletions src/commands/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Utils from "../utils/index";
const Shell = require("shelljs");
const chalk = require("chalk");
const privatePrefix = "@xsyx";
const privateResource = "https://mirrors.xsyxxd.com/repository/npm-group/";

interface Mode {
[key: string]: boolean;
}
declare interface EachIterator {
key: string;
index: number;
array: Array<any>;
originObject: Record<string, any>;
}
module.exports = (arg: string, mode: Mode) => {
const pkg = require(`${process.cwd()}/package.json`);
console.log(chalk.green("🌵 正在由HaYa-CLI代理安装 npm 包...."));

Utils.each(pkg.dependencies, eachHandler);
Utils.each(pkg.devDependencies, eachHandler);
};

function eachHandler({ key, originObject }: EachIterator) {
const name = key;
// const reg = /^(@xsyx)/
const reg = new RegExp(`(^${privatePrefix}\/)`, "i");
/* 拿到版本号,并去掉修饰符 */
const version = originObject[name];
/* shell 语法: npm install [<@scope>/]<name>@<version range> */
if (reg.test(name)) {
Shell.exec(
`npm install ${name}@"${version}" --registry ${privateResource}`
);
} else {
Shell.exec(
`npm install ${name}@"${version}" --registry https://registry.npmjs.org/`
);
}
}

export {};
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { Command } = require("commander");
const program = new Command();
const chalk = require("chalk");
const createCommand = require("./commands/create");
const installCommand = require("./commands/install");
const serveCommand = require("./commands/serve");
const buildCommand: (mode?: string) => void = require("./commands/build");
declare interface MapAction {
Expand Down Expand Up @@ -55,6 +56,16 @@ program
//require(path.resolve(__dirname, `commands/create`))(...process.argv.slice(3));
createCommand(...process.argv.slice(3));
});
program
.command("install")
.alias("i")
.description("代理npm安装")
.option("-D, --dev", "指定安装到 devDependencies")
.option("-S, --save", "指定安装到 dependencies")
.action((option: any) => {
//require(path.resolve(__dirname, `commands/create`))(...process.argv.slice(3));
installCommand(...process.argv.slice(3), option);
});
program
.command("serve")
.alias("s")
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/rollup-plugin-write.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//import { name as appName, version as appVersion} from "./package.json"
const { safeRequire } = require("../utils/index");

import Utils from "../utils/index";
const path = require("path");
const pkg = safeRequire(`${path.resolve(process.cwd(), "package.json")}`);
const pkg = Utils.safeRequire(`${path.resolve(process.cwd(), "package.json")}`);
const appName = pkg ? pkg.name : "demo";
const appVersion = pkg ? pkg.version : "0.0.0";
const fs = require("fs-extra");
Expand Down
28 changes: 24 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
declare interface Utils {
safeRequire: (path: string) => null | Record<string, any>;
each: (obj: object, iteratorFn: (...args: any) => void) => void;
}
const safeRequire = (path: string) => {
try {
return require(path);
} catch (e) {
return null;
}
};
module.exports = {
safeRequire,
};

export {};
function each(obj: object, iteratorFn: (...args: any) => void) {
Reflect.ownKeys(obj).forEach((key, index, array) => {
iteratorFn({
key,
index,
array,
originObject: obj,
});
});
}

// module.exports = {
// safeRequire,
// each
// };

export default {
safeRequire,
each,
} as Utils;
1 change: 1 addition & 0 deletions types/commands/install.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare const Command: any;
declare const program: any;
declare const chalk: any;
declare const createCommand: any;
declare const installCommand: any;
declare const serveCommand: any;
declare const buildCommand: (mode?: string) => void;
declare interface MapAction {
Expand Down
44 changes: 1 addition & 43 deletions types/plugins/rollup-plugin-write.d.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1 @@
declare const safeRequire: any;
declare const path: any;
declare const pkg: any;
declare const appName: any;
declare const appVersion: any;
declare const fs: any;
declare const v: Date;
declare const buildDate: () => string;
declare const defaultInject: () => string;
interface WriteOptions {
injectData?: string | Function;
position?: POSITION;
fileName?: string;
filePath?: string;
content?: string;
}
declare enum POSITION {
Banner = "banner",
Footer = "footer"
}
/**
* rules .css:
* - type = assets
* - data = source
*
* rules .js:
* - type = chunk
* - data = code
*/
declare const CSS_RULE: {
type: string;
data: string;
};
declare const JS_RULE: {
type: string;
data: string;
};
declare function writeBuildInfoInsteadRollUpOptionsForVite(opts?: WriteOptions): {
name: string;
generateBundle: (options: any, bundle: any, isWrite: boolean) => Promise<void>;
writeBundle: () => Promise<void>;
};
declare function findAssetFileInjectCode(assetInfo: any, ext: string, injectData: string | Function, position: string): void;
export {};
7 changes: 6 additions & 1 deletion types/utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export {};
declare interface Utils {
safeRequire: (path: string) => null | Record<string, any>;
each: (obj: object, iteratorFn: (...args: any) => void) => void;
}
declare const _default: Utils;
export default _default;

0 comments on commit 0e557a6

Please sign in to comment.