diff --git a/packages/material-parser/package.json b/packages/material-parser/package.json index b4ec3c43d..dfa1c37aa 100644 --- a/packages/material-parser/package.json +++ b/packages/material-parser/package.json @@ -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", diff --git a/packages/material-parser/scripts/transform.js b/packages/material-parser/scripts/transform.js index 2f9416675..4e9c127f1 100644 --- a/packages/material-parser/scripts/transform.js +++ b/packages/material-parser/scripts/transform.js @@ -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); diff --git a/packages/material-parser/src/core/index.ts b/packages/material-parser/src/core/index.ts new file mode 100644 index 000000000..71f4f052e --- /dev/null +++ b/packages/material-parser/src/core/index.ts @@ -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(); diff --git a/packages/material-parser/src/otter-core/schema/types.ts b/packages/material-parser/src/core/schema/types.ts similarity index 100% rename from packages/material-parser/src/otter-core/schema/types.ts rename to packages/material-parser/src/core/schema/types.ts diff --git a/packages/material-parser/src/generate.ts b/packages/material-parser/src/generate.ts index 61bdce4a3..2ce4d55d6 100644 --- a/packages/material-parser/src/generate.ts +++ b/packages/material-parser/src/generate.ts @@ -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 { diff --git a/packages/material-parser/src/index.ts b/packages/material-parser/src/index.ts index a17a9d51a..b39634182 100644 --- a/packages/material-parser/src/index.ts +++ b/packages/material-parser/src/index.ts @@ -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 { +export default async function (options: IMaterializeOptions): Promise { 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, }); diff --git a/packages/material-parser/src/localize.ts b/packages/material-parser/src/localize.ts index c37971ad6..55d15c30e 100644 --- a/packages/material-parser/src/localize.ts +++ b/packages/material-parser/src/localize.ts @@ -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'); diff --git a/packages/material-parser/src/otter-core/OtterError.ts b/packages/material-parser/src/otter-core/OtterError.ts deleted file mode 100644 index 2435dbf2d..000000000 --- a/packages/material-parser/src/otter-core/OtterError.ts +++ /dev/null @@ -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; diff --git a/packages/material-parser/src/otter-core/index.ts b/packages/material-parser/src/otter-core/index.ts deleted file mode 100644 index 7eedebf65..000000000 --- a/packages/material-parser/src/otter-core/index.ts +++ /dev/null @@ -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; diff --git a/packages/material-parser/src/otter-core/types.ts b/packages/material-parser/src/otter-core/types.ts deleted file mode 100644 index 27a896671..000000000 --- a/packages/material-parser/src/otter-core/types.ts +++ /dev/null @@ -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; // 组件 - }; -} diff --git a/packages/material-parser/src/scan.ts b/packages/material-parser/src/scan.ts index 73ff09044..2b1a09718 100644 --- a/packages/material-parser/src/scan.ts +++ b/packages/material-parser/src/scan.ts @@ -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 { +export default async function scan(options: IMaterializeOptions & { root: string }): Promise { const model: IMaterialScanModel = { pkgName: '', pkgVersion: '', @@ -16,38 +16,34 @@ export default async function scan(options: IMaterializeOptions): Promise