@ywfe/cli
Version:
遥望前端开发命令行工具
132 lines (131 loc) • 5.78 kB
JavaScript
;
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) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateLibName = exports.getFiles = exports.upload = exports.getJsOSSUrl = exports.publishPage = exports.CDN_HOST = void 0;
const vite_1 = require("./vite");
const fs_1 = require("fs");
const path_1 = require("path");
const config_1 = require("./config");
const path = require("path");
const OSS = require("ali-oss");
const chalk_1 = __importDefault(require("chalk"));
const messages_1 = require("./messages");
const devops_1 = require("../service/devops");
exports.CDN_HOST = 'https://ywfedps.oss-cn-hangzhou.aliyuncs.com';
const publishPage = (item, allPageNextVersion, env, buildPath, dpsEnv, currentBranch, commitId) => __awaiter(void 0, void 0, void 0, function* () {
const { page_id: pageId, file_path: pageName, develop_type: developType, oss_schema_url: ossSchemaUrl } = item;
const assetEnvNumber = env === 'prod' ? 2 : 1;
const nextVersion = allPageNextVersion[pageId];
if (developType === 2) {
const res = yield (0, devops_1.publishFn)({
page_id: pageId,
asset_env: assetEnvNumber,
version: env === 'prod' ? nextVersion : '',
commit_id: commitId,
url: ossSchemaUrl,
}, { dpsEnv: dpsEnv });
if (res === null || res === void 0 ? void 0 : res.success) {
return `${exports.CDN_HOST}${ossSchemaUrl}`;
}
}
else {
const libName = (0, exports.generateLibName)(pageName);
const files = (0, exports.getFiles)(buildPath, libName);
const pkgPath = (0, path_1.join)(process.cwd(), 'package.json');
const pkg = require(pkgPath);
const pkgName = pkg.name;
const preFileName = env === 'daily' && currentBranch ? `${env}/${pkgName}/${currentBranch}/${commitId}/` : `${env}/${pkgName}/${nextVersion}/`;
const ossUrl = yield (0, exports.upload)(files, preFileName);
if (ossUrl && ossUrl.length) {
const url = (0, exports.getJsOSSUrl)(ossUrl);
const ossPath = url === null || url === void 0 ? void 0 : url.replace(exports.CDN_HOST, '');
const res = yield (0, devops_1.publishFn)({
page_id: pageId,
asset_env: assetEnvNumber,
version: env === 'prod' ? nextVersion : '',
commit_id: commitId,
url: ossPath,
}, { dpsEnv: dpsEnv });
if (res === null || res === void 0 ? void 0 : res.success) {
return `${exports.CDN_HOST}${ossPath}`;
}
}
}
});
exports.publishPage = publishPage;
const getJsOSSUrl = (urlArr) => {
var _a;
if (Array.isArray(urlArr)) {
return (_a = urlArr.filter((url) => {
return url.lastIndexOf('.js') > -1;
})) === null || _a === void 0 ? void 0 : _a[0];
}
};
exports.getJsOSSUrl = getJsOSSUrl;
const getOSSInfo = () => __awaiter(void 0, void 0, void 0, function* () {
const ywfeConfigYApi = (0, config_1.getYWFEConfig)('dps');
return ywfeConfigYApi;
});
const upload = (files, preFileName) => __awaiter(void 0, void 0, void 0, function* () {
const { bucket, region, accessKeyId, accessKeySecret } = yield getOSSInfo();
const client = new OSS({
secure: true,
region,
accessKeyId,
accessKeySecret,
});
client.useBucket(bucket);
const uploadFile = (file) => __awaiter(void 0, void 0, void 0, function* () {
const fileName = path.basename(file);
return yield client.put(`${preFileName}${fileName}`, file);
});
try {
const results = yield Promise.all(files.map(uploadFile));
return results.map((res) => {
return res.url;
});
}
catch (e) {
console.log(chalk_1.default.red(messages_1.MESSAGES.OSS_UPLOAD_FAIL_TIPS), e);
return false;
}
});
exports.upload = upload;
const getFiles = (filePath, pageName) => {
const result = [];
const tempArr = pageName.split('/');
let tempPageName = tempArr.pop() || '';
tempPageName = tempPageName.replace(/-/g, '');
const findFile = (path) => {
let files = (0, fs_1.readdirSync)(path);
files.forEach(function (item, index) {
let fPath = (0, path_1.join)(path, item);
const arr = item.split('/');
const itemFileName = arr[arr.length - 1];
const itemPageName = itemFileName.split('.')[0];
if (itemPageName === tempPageName || fPath.indexOf('style.css') > -1) {
result.push(fPath);
}
});
};
findFile(`${filePath}/${tempArr.join('/')}`);
return result;
};
exports.getFiles = getFiles;
const generateLibName = (pageName) => {
const tempArr = pageName.split('/');
const libName = tempArr.map((n) => (0, vite_1.firstLetterToUpperCase)(n, true).replace(/-/g, '')).join('');
return libName;
};
exports.generateLibName = generateLibName;