Skip to content

Commit

Permalink
Merge pull request #86 from Serverless-Devs/develop
Browse files Browse the repository at this point in the history
pre 0.1.44
  • Loading branch information
xsahxl authored Jul 20, 2022
2 parents a90ab73 + a971a48 commit de63ec5
Show file tree
Hide file tree
Showing 11 changed files with 720 additions and 494 deletions.
1,066 changes: 616 additions & 450 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/core",
"version": "0.1.43",
"version": "0.1.44",
"description": "Serverless Devs Tool Core Component",
"keywords": [
"Serverless",
Expand Down Expand Up @@ -43,6 +43,7 @@
"main": "./dist/index.js",
"devDependencies": {
"@types/archiver": "^5.1.1",
"@types/command-exists": "^1.2.0",
"@types/command-line-usage": "^5.0.1",
"@types/decompress": "^4.2.4",
"@types/fs-extra": "^9.0.13",
Expand All @@ -57,7 +58,7 @@
"jest": "^27.4.6",
"ts-jest": "^27.1.2",
"ts-node": "^10.4.0",
"typescript": "^4.1.3"
"typescript": "4.1.3"
},
"husky": {
"hooks": {
Expand All @@ -78,6 +79,7 @@
"archiver": "^5.3.0",
"art-template": "^4.13.2",
"chalk": "^4.1.0",
"command-exists": "^1.2.9",
"command-line-usage": "^6.1.1",
"crypto-js": "^4.0.0",
"decompress": "^4.2.1",
Expand All @@ -100,7 +102,9 @@
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"string-argv": "^0.3.1",
"strip-dirs": "^3.0.0",
"table-layout": "^1.0.2",
"walk-sync": "^3.0.0",
"yaml": "^1.10.2"
}
}
2 changes: 1 addition & 1 deletion publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Component
Name: core
Provider:
- 阿里云
Version: 0.1.43
Version: 0.1.44
Description: Serverless Devs 核心组件
HomePage: https://github.com/Serverless-Devs/core
Tags: #标签详情
Expand Down
32 changes: 31 additions & 1 deletion src/common/downloadRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import path from 'path';
import { URL } from 'url';
import decompress, { DecompressOptions } from 'decompress';
import report from '../common/report';
import commandExists from 'command-exists';
import execa from 'execa';
import stripDirs from 'strip-dirs';
import walkSync from 'walk-sync';
import rimraf from 'rimraf';

export interface IOptions extends DecompressOptions {
/**
* If set to true, try extracting the file using decompress.
Expand Down Expand Up @@ -79,10 +85,34 @@ export default async (url: string, dest: string, options: IOptions = {}) => {
}
spin.text = filename ? `${filename} file unzipping${str}` : `file unzipping${str}`;
}, 300);

let useSystemUnzip = false;
if (process.env['default_serverless_devs_system_unzip'] === 'true') {
useSystemUnzip = commandExists.sync('unzip');
}
const tmpDir = path.join(path.dirname(dest), `devsapp-package-${Date.now()}`);
// node-v12.22.1: end of central directory record signature not found
for (let index = 0; index < 3; index++) {
try {
await decompress(filePath, dest, restOpts);
if (useSystemUnzip) {
if (restOpts?.strip) {
execa.sync('unzip', ['-d', tmpDir, '-o', filePath]);
const paths = walkSync(tmpDir);
for (const p of paths) {
const fillPath = path.join(tmpDir, p);
const stat = fs.statSync(fillPath);
if (stat.isFile()) {
const stripPath = stripDirs(p, restOpts.strip);
fs.moveSync(fillPath, path.join(dest, stripPath), { overwrite: true });
}
}
rimraf.sync(tmpDir);
} else {
execa.sync('unzip', ['-d', dest, '-o', filePath]);
}
} else {
await decompress(filePath, dest, restOpts);
}
clearInterval(timer);
await fs.unlink(filePath);
const text = 'file decompression completed';
Expand Down
27 changes: 1 addition & 26 deletions src/common/execCommand/getTemplatePath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs-extra';
import { getYamlContent } from '../../libs';
import { getYamlContent, isYamlFile } from '../../libs';
import { first, get, isEmpty, isPlainObject, omit } from 'lodash';
import yaml from 'js-yaml';
import chalk from 'chalk';
Expand Down Expand Up @@ -69,31 +69,6 @@ async function setupEnv(templateFile: string) {
}
}

async function isYamlFile(filePath: string) {
if (!fs.existsSync(filePath)) {
throw new Error(`${filePath} file was not found.`);
}
const arr = ['.yaml', '.yml'];
if (!arr.includes(path.extname(filePath))) {
throw new Error(`${filePath} file should be yaml or yml file.`);
}
try {
await yaml.load(fs.readFileSync(filePath, 'utf8'));
} catch (error /* YAMLException */) {
const filename = path.basename(filePath);
let message = `${filename} format is incorrect`;
if (error.message) message += `: ${error.message}`;
throw new Error(
JSON.stringify({
message,
tips: `Please check the configuration of ${filename}, Serverless Devs' Yaml specification document can refer to:${chalk.underline(
'https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/yaml.md',
)}`,
}),
);
}
}

async function extendsYaml(spath: string, dotspath: string, data: any) {
const extendsPath = data?.extends ? first(data.extends) : undefined;
let yamlPath = data?.extend ? data.extend : extendsPath;
Expand Down
38 changes: 30 additions & 8 deletions src/common/load/loadApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import chalk from 'chalk';
import _, { get, isEmpty, sortBy, includes, map, concat } from 'lodash';
import rimraf from 'rimraf';
import installDependency from '../installDependency';
import { readJsonFile, getYamlContent, S_CURRENT, getSetConfig } from '../../libs';
import { readJsonFile, getYamlContent, S_CURRENT, getSetConfig, isYamlFile } from '../../libs';
import { getCredentialAliasList, setCredential } from '../credential';
import { replaceFun, getYamlPath, getTemplatekey } from './utils';
import parse from './parse';
Expand Down Expand Up @@ -207,18 +207,33 @@ class LoadApplication {
const requiredList = get(publishYamlData, 'Parameters.required');
const promptList = [];
if (properties) {
const rangeLeft = [];
const rangeRight = [];
let rangeList = [];
for (const key in properties) {
const ele = properties[key];
const newEle = { ...ele, _key: key };
'x-range' in ele ? rangeLeft.push(newEle) : rangeRight.push(newEle);
ele['_key'] = key;
rangeList.push(ele);
}

const rangeList = sortBy(rangeLeft, (o) => o['x-range']).concat(rangeRight);
rangeList = sortBy(rangeList, (o) => o['x-range']);
for (const item of rangeList) {
const name = item._key;
if (item.enum) {
// 布尔类型
if (item.type === 'boolean') {
promptList.push({
type: 'confirm',
name,
message: item.description,
default: item.default,
});
} else if (item.type === 'password') {
// 密码类型
promptList.push({
type: 'password',
name,
message: item.description,
default: item.default,
});
} else if (item.enum) {
// 枚举类型
promptList.push({
type: 'list',
name,
Expand All @@ -228,6 +243,7 @@ class LoadApplication {
default: item.default,
});
} else if (item.type === 'string') {
// 字符串类型
promptList.push({
type: 'input',
message: item.title,
Expand Down Expand Up @@ -281,6 +297,10 @@ class LoadApplication {
}
artTemplate.defaults.extname = path.extname(spath);
let newData = artTemplate(spath, result);
// art 语法需要先解析在验证yaml内容
fs.writeFileSync(spath, newData, 'utf-8');
// fix: Document with errors cannot be stringified
await isYamlFile(spath);
newData = parse({ appName: this.config.appName }, newData);
fs.writeFileSync(spath, newData, 'utf-8');
}
Expand Down Expand Up @@ -309,6 +329,8 @@ class LoadApplication {
...newObj,
...accessObj,
});
fs.writeFileSync(spath, newData, 'utf-8');
await isYamlFile(spath);
newData = parse({ appName: this.config.appName }, newData);
fs.writeFileSync(spath, newData, 'utf-8');
}
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/constant.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
DEFAULT_CORE_VERSION: '0.1.43',
DEFAULT_CORE_VERSION: '0.1.44',
};
1 change: 1 addition & 0 deletions src/libs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './utils';
export * from './common';
export { default as getYamlContent } from './getYamlContent';
export { default as isYamlFile } from './isYamlFile';
31 changes: 31 additions & 0 deletions src/libs/isYamlFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fs from 'fs-extra';
import path from 'path';
import yaml from 'js-yaml';
import chalk from 'chalk';

async function isYamlFile(filePath: string) {
if (!fs.existsSync(filePath)) {
throw new Error(`${filePath} file was not found.`);
}
const arr = ['.yaml', '.yml'];
if (!arr.includes(path.extname(filePath))) {
throw new Error(`${filePath} file should be yaml or yml file.`);
}
try {
await yaml.load(fs.readFileSync(filePath, 'utf8'));
} catch (error /* YAMLException */) {
const filename = path.basename(filePath);
let message = `${filename} format is incorrect`;
if (error.message) message += `: ${error.message}`;
throw new Error(
JSON.stringify({
message,
tips: `Please check the configuration of ${filename}, Serverless Devs' Yaml specification document can refer to:${chalk.underline(
'https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/yaml.md',
)}`,
}),
);
}
}

export default isYamlFile;
5 changes: 1 addition & 4 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ function strip(value: string) {
function logWrite(data) {
const filePath = getLogPath();
if (filePath) {
const instance = fs.createWriteStream(filePath, { flags: 'a' });
instance.on('open', () => {
instance.write(strip(data));
});
fs.appendFileSync(filePath, strip(data))
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/downloadRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { downloadRequest } from "../../src";
(async () => {
// https://registry.devsapp.cn/simple/devsapp/strapi/zipball/0.0.9
// https://registry.devsapp.cn/simple/devsapp/core/zipball/0.1.31
await downloadRequest('https://registry.devsapp.cn/simple/devsapp/strapi/zipball/0.0.9', './.s/core', {
await downloadRequest('https://registry.devsapp.cn/simple/devsapp/core/zipball/0.1.31', './.s/core', {
extract: true,
strip: 1,
filename: 'xx.zip'
Expand Down

0 comments on commit de63ec5

Please sign in to comment.