Merge branch 'feat/material-parser-build-plugin' into 'release/1.0.0'

feat: add root field to material parser options

1. 支持options中的root字段
2. 删除otter-core中无关代码,并更名为core
3. 更新snapshot

See merge request !950598
This commit is contained in:
彼洋 2020-08-26 17:16:10 +08:00
commit 6f4af00959
19 changed files with 179 additions and 163 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@ali/lowcode-material-parser", "name": "@ali/lowcode-material-parser",
"version": "1.0.5-0", "version": "1.0.5-1",
"description": "material parser for Ali lowCode engine", "description": "material parser for Ali lowCode engine",
"main": "lib/index.js", "main": "lib/index.js",
"files": [ "files": [
@ -15,6 +15,7 @@
"@types/fs-extra": "^8.0.1", "@types/fs-extra": "^8.0.1",
"@types/js-yaml": "^3.12.2", "@types/js-yaml": "^3.12.2",
"@types/lodash": "^4.14.149", "@types/lodash": "^4.14.149",
"@types/node": "^14.6.0",
"@types/prop-types": "^15.7.3", "@types/prop-types": "^15.7.3",
"@types/semver": "^7.1.0", "@types/semver": "^7.1.0",
"ava": "3.8.1", "ava": "3.8.1",

View File

@ -8,10 +8,10 @@ const ajv = new Ajv();
const YamlPath = path.resolve(__dirname, '../schemas/schema.yml'); const YamlPath = path.resolve(__dirname, '../schemas/schema.yml');
const JsonPath = path.resolve(__dirname, '../src/validate/schema.json'); const JsonPath = path.resolve(__dirname, '../src/validate/schema.json');
const tsPath = path.resolve(__dirname, '../src/otter-core/schema/types.ts'); const tsPath = path.resolve(__dirname, '../src/core/schema/types.ts');
// Get document, or throw exception on error // Get document, or throw exception on error
(async function() { (async function () {
try { try {
const schema = yaml.load(fs.readFileSync(YamlPath, 'utf8')); const schema = yaml.load(fs.readFileSync(YamlPath, 'utf8'));
ajv.compile(schema); ajv.compile(schema);

View File

@ -0,0 +1,10 @@
import _debug from 'debug';
export * from './schema/types';
/**
* Dev helper
*/
export const debug = _debug('lowcode');
export const enableDebug = () => _debug.enable('lowcode:*');
export const disableDebug = () => _debug.disable();

View File

@ -1,9 +1,9 @@
import { debug, ComponentMeta } from './otter-core'; import { debug, ComponentMeta } from './core';
import { IMaterialParsedModel, IMaterialScanModel } from './types'; import { IMaterialParsedModel, IMaterialScanModel } from './types';
const log = debug.extend('mat'); const log = debug.extend('mat');
export default async function( export default async function (
matScanModel: IMaterialScanModel, matScanModel: IMaterialScanModel,
matParsedModels: IMaterialParsedModel[], matParsedModels: IMaterialParsedModel[],
): Promise<ComponentMeta[]> { ): Promise<ComponentMeta[]> {

View File

@ -1,31 +1,47 @@
import { remove } from 'fs-extra'; import { remove, lstatSync } from 'fs-extra';
export { default as validate } from './validate'; export { default as validate } from './validate';
export { default as schema } from './validate/schema.json'; export { default as schema } from './validate/schema.json';
export * from './types'; export * from './types';
import { IMaterializeOptions } from './types'; import { IMaterializeOptions } from './types';
import { ComponentMeta } from './otter-core'; import { ComponentMeta } from './core';
import scan from './scan'; import scan from './scan';
import generate from './generate'; import generate from './generate';
import parse from './parse'; import parse from './parse';
import localize from './localize'; import localize from './localize';
export default async function(options: IMaterializeOptions): Promise<ComponentMeta[]> { export default async function (options: IMaterializeOptions): Promise<ComponentMeta[]> {
const { accesser = 'local', entry = '' } = options; const { accesser = 'local', entry = '' } = options;
let workDir = entry; let { root } = options;
if (!root && accesser === 'local') {
const stats = lstatSync(entry);
if (stats.isDirectory()) {
root = entry;
} else {
root = process.cwd();
}
}
const internalOptions = {
...options,
root,
};
let workDir = root || '';
let moduleDir = ''; let moduleDir = '';
if (accesser === 'online') { if (accesser === 'online') {
const result = await localize(options); const result = await localize(internalOptions);
workDir = result.workDir; workDir = result.workDir;
moduleDir = result.moduleDir; moduleDir = result.moduleDir;
options.entry = moduleDir; internalOptions.entry = moduleDir;
internalOptions.root = moduleDir;
} }
const scanedModel = await scan(options); const scanedModel = await scan(internalOptions as IMaterializeOptions & { root: string });
const parsedModel = await parse({ const parsedModel = await parse({
...scanedModel, ...scanedModel,
accesser, accesser,
npmClient: options.npmClient, npmClient: internalOptions.npmClient,
workDir, workDir,
moduleDir, moduleDir,
}); });

View File

@ -3,7 +3,7 @@ import { ensureDir, ensureFile, writeFile } from 'fs-extra';
import { join } from 'path'; import { join } from 'path';
import semver from 'semver'; import semver from 'semver';
import uuid from 'short-uuid'; import uuid from 'short-uuid';
import { debug, OtterError } from './otter-core'; import { debug } from './core';
import { IMaterializeOptions } from './types'; import { IMaterializeOptions } from './types';
const log = debug.extend('mat'); const log = debug.extend('mat');

View File

@ -1,70 +0,0 @@
import { _ } from './index';
import { IOtterErrorOptions } from './types';
/**
* Fix the prototype chain of the error
*
* Use Object.setPrototypeOf
* Support ES6 environments
*
* Fallback setting __proto__
* Support IE11+, see https://docs.microsoft.com/en-us/scripting/javascript/reference/javascript-version-information
*/
function fixPrototype(target: Error, prototype: {}) {
const setPrototypeOf: typeof Object.setPrototypeOf = (Object as any)
.setPrototypeOf;
setPrototypeOf
? setPrototypeOf(target, prototype)
: ((target as any).__proto__ = prototype);
}
/**
* Capture and fix the error stack when available
*
* Use Error.captureStackTrace
* Support v8 environments
*/
function fixStackTrace(target: Error, fn: any = target.constructor) {
const captureStackTrace: any = (Error as any).captureStackTrace;
if (captureStackTrace) {
captureStackTrace(target, fn);
}
}
class OtterError extends Error {
public name: string = '';
public urlRoot: string = 'https://docs.aimake.io/otter/';
private options: IOtterErrorOptions = {
url: '/',
version: '0.0.0',
};
constructor(message?: string, options?: IOtterErrorOptions) {
super(message);
// set error name as constructor name, make it not enumerable to keep native Error behavior
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target#new.target_in_constructors
Object.defineProperty(this, 'name', {
value: new.target.name,
enumerable: false,
});
// fix the extended error prototype chain
// because typescript __extends implementation can't
// see https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
fixPrototype(this, new.target.prototype);
// try to remove constructor from stack trace
fixStackTrace(this);
_.extend(this.options, options || {});
}
public toString() {
const url = this.urlRoot + this.options.version + this.options.url;
return `${this.name}: ${this.message} See: ${url}`;
}
}
export default OtterError;

View File

@ -1,21 +0,0 @@
import _debug from 'debug';
import _lodash from 'lodash';
import _OtterError from './OtterError';
export * from './types';
export * from './schema/types';
/**
* Dev helper
*/
export const debug = _debug('otter');
export const enableDebug = () => _debug.enable('otter:*');
export const disableDebug = () => _debug.disable();
export const OtterError = _OtterError;
/**
* Dev utils
*/
export const _ = _lodash;

View File

@ -1,22 +0,0 @@
/**
*
* @export
* @interface IOtterErrorOptions
*/
export interface IOtterErrorOptions {
url?: string;
version?: string;
}
/**
*
*
* @export
* @interface IComponents
*/
export interface IComponents {
[componentPackage: string]: {
// 组件包名称
[componentName: string]: any; // 组件
};
}

View File

@ -1,11 +1,11 @@
import { IMaterializeOptions, IMaterialScanModel } from './types'; import { IMaterializeOptions, IMaterialScanModel } from './types';
import { pathExists, lstatSync } from 'fs-extra'; import { pathExists, lstatSync } from 'fs-extra';
import { join, isAbsolute, resolve } from 'path'; import { join, isAbsolute, resolve } from 'path';
import { debug } from './otter-core'; import { debug } from './core';
import { resolvePkgJson } from './utils'; import { resolvePkgJson } from './utils';
const log = debug.extend('mat'); const log = debug.extend('mat');
export default async function scan(options: IMaterializeOptions): Promise<IMaterialScanModel> { export default async function scan(options: IMaterializeOptions & { root: string }): Promise<IMaterialScanModel> {
const model: IMaterialScanModel = { const model: IMaterialScanModel = {
pkgName: '', pkgName: '',
pkgVersion: '', pkgVersion: '',
@ -16,38 +16,34 @@ export default async function scan(options: IMaterializeOptions): Promise<IMater
// 入口文件路径 // 入口文件路径
let entryFilePath = options.entry; let entryFilePath = options.entry;
const stats = lstatSync(entryFilePath); const stats = lstatSync(entryFilePath);
if (!stats.isFile()) { if (options.accesser === 'local' && stats.isFile()) {
const pkgJsonPath = join(entryFilePath, 'package.json'); if (isAbsolute(entryFilePath)) {
// 判断是否存在 package.json model.mainFilePath = entryFilePath;
if (!(await pathExists(pkgJsonPath))) { model.mainFileAbsolutePath = entryFilePath;
throw new Error(`Cannot find package.json. ${pkgJsonPath}`); } else {
model.mainFilePath = entryFilePath;
model.mainFileAbsolutePath = resolve(entryFilePath);
} }
// 读取 package.json }
const pkgJsonPath = join(options.root, 'package.json');
if (await pathExists(pkgJsonPath)) {
let pkgJson = await resolvePkgJson(pkgJsonPath); let pkgJson = await resolvePkgJson(pkgJsonPath);
model.pkgName = pkgJson.name; model.pkgName = pkgJson.name;
model.pkgVersion = pkgJson.version; model.pkgVersion = pkgJson.version;
if (pkgJson.module) { if (pkgJson.module) {
model.moduleFilePath = pkgJson.module; model.moduleFilePath = pkgJson.module;
model.moduleFileAbsolutePath = join(entryFilePath, pkgJson.module); model.moduleFileAbsolutePath = join(options.root, pkgJson.module);
} }
model.mainFilePath = pkgJson.main || './index.js'; model.mainFilePath = model.mainFilePath || pkgJson.main || './index.js';
model.mainFileAbsolutePath = join(entryFilePath, pkgJson.main); model.mainFileAbsolutePath = model.mainFileAbsolutePath || join(entryFilePath, pkgJson.main);
const typingsFilePath = pkgJson.typings || pkgJson.types || './lib/index.d.ts'; const typingsFilePath = pkgJson.typings || pkgJson.types || './lib/index.d.ts';
const typingsFileAbsolutePath = join(entryFilePath, typingsFilePath); const typingsFileAbsolutePath = join(entryFilePath, typingsFilePath);
if (await pathExists(typingsFileAbsolutePath)) { if (await pathExists(typingsFileAbsolutePath)) {
model.typingsFileAbsolutePath = typingsFileAbsolutePath; model.typingsFileAbsolutePath = typingsFileAbsolutePath;
model.typingsFilePath = typingsFilePath; model.typingsFilePath = typingsFilePath;
} }
} else if (isAbsolute(entryFilePath)) {
model.mainFilePath = entryFilePath;
model.mainFileAbsolutePath = entryFilePath;
} else {
model.mainFilePath = entryFilePath;
model.mainFileAbsolutePath = resolve(entryFilePath);
} }
// 记录入口文件
log('model', model); log('model', model);
return model; return model;
} }

View File

@ -1,4 +1,4 @@
import { ComponentMeta } from '../otter-core'; import { ComponentMeta } from '../core';
/** /**
* *

View File

@ -1,4 +1,4 @@
import { ComponentMeta } from '../otter-core'; import { ComponentMeta } from '../core';
/** /**
* manifest * manifest
* *

View File

@ -1,4 +1,4 @@
import { PropsSection } from '../otter-core'; import { PropsSection } from '../core';
/** /**
* *
*/ */

View File

@ -14,6 +14,14 @@ interface IMaterializeOptions {
*/ */
entry: string; entry: string;
/**
* entry为文件路径的时候root来指定根目录entry为文件夹时root默认为entry
*
* ./
* /usr/project/src/container/DemoMaterial
*/
root?: string;
/** /**
* *
* localonline线 npm * localonline线 npm
@ -22,18 +30,6 @@ interface IMaterializeOptions {
*/ */
accesser: 'local' | 'online'; accesser: 'local' | 'online';
/**
*
*/
mode?: 'static' | 'dynamic' | 'mixed' | 'auto'
/**
*
*/
extensions?: {
[ExtensionName.CONFIGMANIFEST]?: IExtensionConfigManifest;
};
/** /**
* accesser=online 使 npm clienttnpmcnpmyarnnpm * accesser=online 使 npm clienttnpmcnpmyarnnpm
*/ */

View File

@ -1376,10 +1376,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -5054,10 +5059,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -8209,10 +8219,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -10880,10 +10895,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -14175,10 +14195,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -16118,10 +16143,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -17658,10 +17688,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -20252,10 +20287,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -24945,10 +24985,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -26979,10 +27024,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -33410,10 +33460,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -34956,10 +35011,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -39122,10 +39182,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -41110,10 +41175,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -43098,10 +43168,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -45801,10 +45876,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -48577,10 +48657,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -52504,10 +52589,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -54270,10 +54360,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -55817,10 +55912,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -57732,10 +57832,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },
@ -58998,10 +59103,15 @@ Generated by [AVA](https://avajs.dev).
type: 'oneOfType', type: 'oneOfType',
value: [ value: [
'additions', 'additions',
'additions removals',
'additions text', 'additions text',
'all', 'all',
'removals', 'removals',
'removals additions',
'removals text',
'text', 'text',
'text additions',
'text removals',
], ],
}, },
}, },

View File

@ -18,7 +18,7 @@ Generated by [AVA](https://avajs.dev).
main: 'lib/index.js', main: 'lib/index.js',
package: 'mc-breadcrumb', package: 'mc-breadcrumb',
subName: '', subName: '',
version: '1.0.0', version: '1.0.1',
}, },
props: [ props: [
{ {
@ -124,7 +124,7 @@ Generated by [AVA](https://avajs.dev).
main: 'lib/index.js', main: 'lib/index.js',
package: 'mc-breadcrumb', package: 'mc-breadcrumb',
subName: 'Item', subName: 'Item',
version: '1.0.0', version: '1.0.1',
}, },
props: [ props: [
{ {