feat: add root field to material parser options

This commit is contained in:
gengyang 2020-08-26 17:08:35 +08:00
parent 0ecce83a27
commit c6724e9ce3
19 changed files with 179 additions and 163 deletions

View File

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

View File

@ -8,10 +8,10 @@ const ajv = new Ajv();
const YamlPath = path.resolve(__dirname, '../schemas/schema.yml');
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
(async function() {
(async function () {
try {
const schema = yaml.load(fs.readFileSync(YamlPath, 'utf8'));
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';
const log = debug.extend('mat');
export default async function(
export default async function (
matScanModel: IMaterialScanModel,
matParsedModels: IMaterialParsedModel[],
): 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 schema } from './validate/schema.json';
export * from './types';
import { IMaterializeOptions } from './types';
import { ComponentMeta } from './otter-core';
import { ComponentMeta } from './core';
import scan from './scan';
import generate from './generate';
import parse from './parse';
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;
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 = '';
if (accesser === 'online') {
const result = await localize(options);
const result = await localize(internalOptions);
workDir = result.workDir;
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({
...scanedModel,
accesser,
npmClient: options.npmClient,
npmClient: internalOptions.npmClient,
workDir,
moduleDir,
});

View File

@ -3,7 +3,7 @@ import { ensureDir, ensureFile, writeFile } from 'fs-extra';
import { join } from 'path';
import semver from 'semver';
import uuid from 'short-uuid';
import { debug, OtterError } from './otter-core';
import { debug } from './core';
import { IMaterializeOptions } from './types';
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 { pathExists, lstatSync } from 'fs-extra';
import { join, isAbsolute, resolve } from 'path';
import { debug } from './otter-core';
import { debug } from './core';
import { resolvePkgJson } from './utils';
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 = {
pkgName: '',
pkgVersion: '',
@ -16,38 +16,34 @@ export default async function scan(options: IMaterializeOptions): Promise<IMater
// 入口文件路径
let entryFilePath = options.entry;
const stats = lstatSync(entryFilePath);
if (!stats.isFile()) {
const pkgJsonPath = join(entryFilePath, 'package.json');
// 判断是否存在 package.json
if (!(await pathExists(pkgJsonPath))) {
throw new Error(`Cannot find package.json. ${pkgJsonPath}`);
if (options.accesser === 'local' && stats.isFile()) {
if (isAbsolute(entryFilePath)) {
model.mainFilePath = entryFilePath;
model.mainFileAbsolutePath = entryFilePath;
} 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);
model.pkgName = pkgJson.name;
model.pkgVersion = pkgJson.version;
if (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.mainFileAbsolutePath = join(entryFilePath, pkgJson.main);
model.mainFilePath = model.mainFilePath || pkgJson.main || './index.js';
model.mainFileAbsolutePath = model.mainFileAbsolutePath || join(entryFilePath, pkgJson.main);
const typingsFilePath = pkgJson.typings || pkgJson.types || './lib/index.d.ts';
const typingsFileAbsolutePath = join(entryFilePath, typingsFilePath);
if (await pathExists(typingsFileAbsolutePath)) {
model.typingsFileAbsolutePath = typingsFileAbsolutePath;
model.typingsFilePath = typingsFilePath;
}
} else if (isAbsolute(entryFilePath)) {
model.mainFilePath = entryFilePath;
model.mainFileAbsolutePath = entryFilePath;
} else {
model.mainFilePath = entryFilePath;
model.mainFileAbsolutePath = resolve(entryFilePath);
}
// 记录入口文件
log('model', 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
*

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

View File

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

View File

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