@eljs/create
Version:
Create a project from a remote template.
143 lines (141 loc) • 4.74 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/core/download.ts
var download_exports = {};
__export(download_exports, {
Download: () => Download
});
module.exports = __toCommonJS(download_exports);
var import_utils = require("@eljs/utils");
var import_node_path = __toESM(require("node:path"));
var import_ora = __toESM(require("ora"));
var Download = class {
/**
* 构造函数选项
*/
constructorOptions;
/**
* spinner
*/
_spinner;
constructor(options) {
this.constructorOptions = options;
this._spinner = (0, import_ora.default)();
}
async download() {
const { type, value, registry } = this.constructorOptions;
switch (type) {
case "npm":
return this._downloadNpmTarball(value, registry);
case "git":
return this._downloadGit(value);
default:
throw new Error(
`Download type must be \`npm\` or \`git\`, but got \`${type}\`.`
);
}
}
/**
* 下载 npm 压缩包
* @param name 包名
* @param registry 仓库源
*/
async _downloadNpmTarball(name, registry) {
const { name: pkgName, version } = (0, import_utils.pkgNameAnalysis)(name);
const data = await (0, import_utils.getNpmPackage)(pkgName, {
cwd: this.constructorOptions.cwd,
version,
registry
});
if (!data) {
throw new Error(
`Access ${pkgName}${version ? `@${version}` : ""} failed.`
);
}
const projectName = import_utils.chalk.cyan(`${pkgName}@${data.version}`);
let templateRootPath = "";
try {
this._spinner.start(`Downloading ${projectName}`);
const { tarball } = data.dist;
templateRootPath = await (0, import_utils.downloadNpmTarball)(tarball);
this._spinner.succeed();
} catch (error) {
this._spinner.fail();
const err = error;
err.message = `Download ${projectName} failed: ${err.message}`;
throw err;
}
await this._installDependencies(templateRootPath, projectName);
return templateRootPath;
}
/**
* 下载 git
* @param url git url
*/
async _downloadGit(url) {
let templateRootPath = "";
try {
this._spinner.start(`Downloading ${url}`);
templateRootPath = await (0, import_utils.downloadGitRepository)(url);
this._spinner.succeed();
} catch (error) {
this._spinner.fail();
const err = error;
err.message = `Download ${url} failed: ${err.message}`;
throw err;
}
await this._installDependencies(templateRootPath, url);
return templateRootPath;
}
/**
* 安装依赖
* @param cwd 当前工作目录
* @param projectName 项目名称
*/
async _installDependencies(cwd, projectName) {
try {
const { dependencies } = await (0, import_utils.readJson)(import_node_path.default.join(cwd, "./package.json")) || {};
if (dependencies && Object.keys(dependencies).length > 0) {
this._spinner.start(`Installing ${projectName}`);
await (0, import_utils.run)("npm", ["install", "--production"], {
cwd
});
this._spinner.succeed();
}
} catch (error) {
this._spinner.fail();
const err = error;
err.message = `Install dependencies in ${projectName} failed: ${err.message}`;
throw err;
}
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Download
});