node-opcua-generator
Advanced tools
Comparing version 2.0.0-alpha.6 to 2.0.0-alpha.7
@@ -594,3 +594,3 @@ "use strict"; | ||
function write(...args) { | ||
f.write.apply(f, arguments); | ||
f.write.apply(f, args); | ||
} | ||
@@ -667,4 +667,4 @@ // Xx resolve_schema_field_types(schema, generatedObjectSchema); | ||
const options = { | ||
parser: "typescript", | ||
printWidth: 120, | ||
parser: "typescript", | ||
insertPragma: true, | ||
@@ -671,0 +671,0 @@ bracketSpacing: true |
@@ -24,3 +24,3 @@ "use strict"; | ||
function write(...args) { | ||
f.write.apply(f, arguments); | ||
f.write.apply(f, args); | ||
} | ||
@@ -228,2 +228,5 @@ function removeNamespacePart(str) { | ||
} from "node-opcua-basic-types"; | ||
import { Enum, EnumItem } from "node-opcua-enum"; | ||
import { BinaryStream } from "node-opcua-binary-stream"; | ||
@@ -230,0 +233,0 @@ import { |
@@ -20,2 +20,3 @@ "use strict"; | ||
const factory_code_generator_1 = require("./factory_code_generator"); | ||
// tslint:disable:no-console | ||
const fileExists = util_1.promisify(fs.exists); | ||
@@ -133,3 +134,3 @@ const mkdir = util_1.promisify(fs.mkdir); | ||
console.log("error !", schema); | ||
//xx process.exit(1); | ||
// xx process.exit(1); | ||
} | ||
@@ -143,3 +144,5 @@ // we expect <schema>|<hint> | ||
hintSchema.unshift(defaultHint); | ||
generateCodeFolder = generateCodeFolder ? generateCodeFolder : path.join(path.dirname(callerFolder), "_generated_"); | ||
generateCodeFolder = generateCodeFolder | ||
? generateCodeFolder | ||
: path.join(path.dirname(callerFolder), "_generated_"); | ||
} | ||
@@ -146,0 +149,0 @@ const folderHint = hintSchema[0]; |
{ | ||
"name": "node-opcua-generator", | ||
"version": "2.0.0-alpha.6", | ||
"version": "2.0.0-alpha.7", | ||
"description": "pure nodejs OPCUA SDK - module -generator", | ||
@@ -13,24 +13,24 @@ "main": "./dist/index.js", | ||
"chalk": "^2.4.1", | ||
"node-opcua-assert": "^2.0.0-alpha.6", | ||
"node-opcua-basic-types": "^2.0.0-alpha.6", | ||
"node-opcua-buffer-utils": "^2.0.0-alpha.6", | ||
"node-opcua-constants": "^2.0.0-alpha.6", | ||
"node-opcua-date-time": "^2.0.0-alpha.6", | ||
"node-opcua-debug": "^2.0.0-alpha.6", | ||
"node-opcua-enum": "^2.0.0-alpha.6", | ||
"node-opcua-factory": "^2.0.0-alpha.6", | ||
"node-opcua-guid": "^2.0.0-alpha.6", | ||
"node-opcua-nodeid": "^2.0.0-alpha.6", | ||
"node-opcua-status-code": "^2.0.0-alpha.6", | ||
"node-opcua-utils": "^2.0.0-alpha.6", | ||
"node-opcua-xml2json": "^2.0.0-alpha.6", | ||
"prettier": "^1.14.3", | ||
"typescript": "^3.1.2", | ||
"node-opcua-assert": "^2.0.0-alpha.7", | ||
"node-opcua-basic-types": "^2.0.0-alpha.7", | ||
"node-opcua-buffer-utils": "^2.0.0-alpha.7", | ||
"node-opcua-constants": "^2.0.0-alpha.7", | ||
"node-opcua-date-time": "^2.0.0-alpha.7", | ||
"node-opcua-debug": "^2.0.0-alpha.7", | ||
"node-opcua-enum": "^2.0.0-alpha.7", | ||
"node-opcua-factory": "^2.0.0-alpha.7", | ||
"node-opcua-guid": "^2.0.0-alpha.7", | ||
"node-opcua-nodeid": "^2.0.0-alpha.7", | ||
"node-opcua-status-code": "^2.0.0-alpha.7", | ||
"node-opcua-utils": "^2.0.0-alpha.7", | ||
"node-opcua-xml2json": "^2.0.0-alpha.7", | ||
"prettier": "^1.15.3", | ||
"typescript": "^3.2.1", | ||
"underscore": "^1.9.1" | ||
}, | ||
"devDependencies": { | ||
"@types/prettier": "^1.13.2", | ||
"node-opcua-binary-stream": "^2.0.0-alpha.6", | ||
"node-opcua-extension-object": "^2.0.0-alpha.6", | ||
"node-opcua-packet-analyzer": "^2.0.0-alpha.6", | ||
"@types/prettier": "^1.15.2", | ||
"node-opcua-binary-stream": "^2.0.0-alpha.7", | ||
"node-opcua-extension-object": "^2.0.0-alpha.7", | ||
"node-opcua-packet-analyzer": "^2.0.0-alpha.7", | ||
"should": "13.2.3", | ||
@@ -54,3 +54,3 @@ "source-map-support": "^0.5.9" | ||
"homepage": "http://node-opcua.github.io/", | ||
"gitHead": "12f2b33c00f146e37797ef9ab2631e868b072bc0" | ||
"gitHead": "af4f00cca7a45563e759c88afa21bba73152dd03" | ||
} |
@@ -686,4 +686,4 @@ import * as fs from "fs"; | ||
function write(...args: any[]) { | ||
f.write.apply(f, arguments); | ||
function write(...args: string[]) { | ||
f.write.apply(f, args); | ||
} | ||
@@ -693,3 +693,4 @@ | ||
const complexTypes = schema.fields.filter((field: FieldType) => field.category === FieldCategory.complex && field.fieldType !== schema.name); | ||
const complexTypes = schema.fields.filter( | ||
(field: FieldType) => field.category === FieldCategory.complex && field.fieldType !== schema.name); | ||
@@ -781,4 +782,4 @@ const folderForSourceFile = path.dirname(generatedTypescriptFilename); | ||
const options: prettier.Options = { | ||
parser: "typescript", | ||
printWidth: 120, | ||
parser: "typescript", | ||
insertPragma: true, | ||
@@ -785,0 +786,0 @@ bracketSpacing: true |
@@ -17,3 +17,3 @@ // | ||
function write(...args: string[]) { | ||
f.write.apply(f, arguments); | ||
f.write.apply(f, args); | ||
} | ||
@@ -260,2 +260,5 @@ | ||
} from "node-opcua-basic-types"; | ||
import { Enum, EnumItem } from "node-opcua-enum"; | ||
import { BinaryStream } from "node-opcua-binary-stream"; | ||
@@ -262,0 +265,0 @@ import { |
@@ -18,2 +18,4 @@ import * as fs from "fs"; | ||
// tslint:disable:no-console | ||
const fileExists = promisify(fs.exists); | ||
@@ -30,24 +32,27 @@ const mkdir = promisify(fs.mkdir); | ||
const content = fs.readFileSync(typescriptFilename, "ascii"); | ||
const content = fs.readFileSync(typescriptFilename, "ascii"); | ||
const compilerOptions = { | ||
module: ts.ModuleKind.CommonJS, | ||
target: ts.ScriptTarget.ES2016, | ||
skipLibCheck: true, | ||
declaration: true, | ||
sourceMap: true, | ||
strict: true, | ||
noImplicitAny: true, | ||
noImplicitReturns: true | ||
}; | ||
const compilerOptions = { | ||
module: ts.ModuleKind.CommonJS, | ||
target: ts.ScriptTarget.ES2016, | ||
const res1 = ts.transpileModule(content, { compilerOptions, moduleName: "myModule2" }); | ||
skipLibCheck: true, | ||
const javascriptFilename = typescriptFilename.replace(/\.ts$/, ".js"); | ||
const sourcemapFilename = typescriptFilename.replace(/\.ts$/, ".js.map"); | ||
declaration: true, | ||
sourceMap: true, | ||
strict: true, | ||
fs.writeFileSync(javascriptFilename, res1.outputText, "ascii"); | ||
fs.writeFileSync(sourcemapFilename, res1.sourceMapText, "ascii"); | ||
noImplicitAny: true, | ||
noImplicitReturns: true | ||
}; | ||
return res1.outputText; | ||
const res1 = ts.transpileModule(content, { compilerOptions, moduleName: "myModule2" }); | ||
const javascriptFilename = typescriptFilename.replace(/\.ts$/, ".js"); | ||
const sourcemapFilename = typescriptFilename.replace(/\.ts$/, ".js.map"); | ||
fs.writeFileSync(javascriptFilename, res1.outputText, "ascii"); | ||
fs.writeFileSync(sourcemapFilename, res1.sourceMapText, "ascii"); | ||
return res1.outputText; | ||
} | ||
@@ -58,95 +63,95 @@ | ||
function get_caller_source_filename() { | ||
// let's find source code where schema file is described | ||
// to do make change this | ||
// the line has the following shape: | ||
// 'at blah (/home/toto/myfile.js:53:34)' | ||
const err = new Error(""); | ||
const re = /.*\((.*):[0-9]*:[0-9]*\)/g; | ||
if (!err.stack) { | ||
return ""; | ||
} | ||
console.log(err.stack.split("\n")); | ||
const ma = err.stack.split("\n"); | ||
let m = re.exec(ma[8]); | ||
if (!m) { | ||
m = re.exec(ma[4]); | ||
} | ||
if (!m) { | ||
return "../"; | ||
// throw new Error("Invalid: cannot find caller_source_filename : " + err.stack + "\n ============="); | ||
} | ||
const schemaFile = m[1]; | ||
return schemaFile; | ||
// let's find source code where schema file is described | ||
// to do make change this | ||
// the line has the following shape: | ||
// 'at blah (/home/toto/myfile.js:53:34)' | ||
const err = new Error(""); | ||
const re = /.*\((.*):[0-9]*:[0-9]*\)/g; | ||
if (!err.stack) { | ||
return ""; | ||
} | ||
console.log(err.stack.split("\n")); | ||
const ma = err.stack.split("\n"); | ||
let m = re.exec(ma[8]); | ||
if (!m) { | ||
m = re.exec(ma[4]); | ||
} | ||
if (!m) { | ||
return "../"; | ||
// throw new Error("Invalid: cannot find caller_source_filename : " + err.stack + "\n ============="); | ||
} | ||
const schemaFile = m[1]; | ||
return schemaFile; | ||
} | ||
export async function generateCode(schemaName: string, localSchemaFile: string, generatedCodeFolder?: string) { | ||
const schemaTypescriptFile = schemaName + "_Schema.ts"; | ||
const schemaTypescriptFile = schemaName + "_Schema.ts"; | ||
const currentFolder = process.cwd(); | ||
// | ||
const localSchemaFileExists = await fileExists(localSchemaFile); | ||
const currentFolder = process.cwd(); | ||
// | ||
const localSchemaFileExists = await fileExists(localSchemaFile); | ||
if (!localSchemaFileExists) { | ||
throw new Error(`Cannot find source file for schema ${schemaTypescriptFile}`); | ||
} | ||
if (!localSchemaFileExists) { | ||
throw new Error(`Cannot find source file for schema ${schemaTypescriptFile}`); | ||
} | ||
if (!generatedCodeFolder) { | ||
generatedCodeFolder = path.join(currentFolder, "_generated_"); | ||
} | ||
if (!generatedCodeFolder) { | ||
generatedCodeFolder = path.join(currentFolder, "_generated_"); | ||
} | ||
const generatedCodeFolderExists = await fileExists(generatedCodeFolder); | ||
if (!generatedCodeFolderExists) { | ||
await mkdir(generatedCodeFolder); | ||
} | ||
const generatedCodeFolderExists = await fileExists(generatedCodeFolder); | ||
if (!generatedCodeFolderExists) { | ||
await mkdir(generatedCodeFolder); | ||
} | ||
const generatedTypescriptSource = path.join(generatedCodeFolder, "_" + schemaName + ".ts"); | ||
const generatedTypescriptSource = path.join(generatedCodeFolder, "_" + schemaName + ".ts"); | ||
const generatedSourceExists = await fileExists(generatedTypescriptSource); | ||
const generatedSourceExists = await fileExists(generatedTypescriptSource); | ||
let schemaFileIsNewer = false; | ||
let codeGeneratorIsNewer = true; | ||
let schemaFileIsNewer = false; | ||
let codeGeneratorIsNewer = true; | ||
if (generatedSourceExists) { | ||
if (generatedSourceExists) { | ||
const generatedSourceMtime = new Date(fs.statSync(generatedTypescriptSource).mtime).getTime(); | ||
const generatedSourceMtime = new Date(fs.statSync(generatedTypescriptSource).mtime).getTime(); | ||
const schemaFileMtime = new Date(fs.statSync(localSchemaFile).mtime).getTime(); | ||
const schemaFileMtime = new Date(fs.statSync(localSchemaFile).mtime).getTime(); | ||
schemaFileIsNewer = (generatedSourceMtime <= schemaFileMtime); | ||
schemaFileIsNewer = (generatedSourceMtime <= schemaFileMtime); | ||
let codeGeneratorScript = path.join(__dirname, "factory_code_generator.ts"); | ||
if (!fs.existsSync(codeGeneratorScript)) { | ||
codeGeneratorScript = path.join(__dirname, "factory_code_generator.js"); | ||
} | ||
let codeGeneratorScript = path.join(__dirname, "factory_code_generator.ts"); | ||
if (!fs.existsSync(codeGeneratorScript)) { | ||
codeGeneratorScript = path.join(__dirname, "factory_code_generator.js"); | ||
} | ||
assert(fs.existsSync(codeGeneratorScript), "cannot get code factory_code_generator" + codeGeneratorScript); | ||
const codeGeneratorScriptMtime = new Date(fs.statSync(codeGeneratorScript).mtime).getTime(); | ||
assert(fs.existsSync(codeGeneratorScript), "cannot get code factory_code_generator" + codeGeneratorScript); | ||
const codeGeneratorScriptMtime = new Date(fs.statSync(codeGeneratorScript).mtime).getTime(); | ||
codeGeneratorIsNewer = (generatedSourceMtime <= codeGeneratorScriptMtime); | ||
} | ||
const generatedSourceIsOutdated = (!generatedSourceExists || codeGeneratorIsNewer || schemaFileIsNewer); | ||
codeGeneratorIsNewer = (generatedSourceMtime <= codeGeneratorScriptMtime); | ||
} | ||
const generatedSourceIsOutdated = (!generatedSourceExists || codeGeneratorIsNewer || schemaFileIsNewer); | ||
if (generatedSourceIsOutdated) { | ||
if (generatedSourceIsOutdated) { | ||
const module = await import(localSchemaFile); | ||
const schema = module[schemaName + "_Schema"]; | ||
const module = await import(localSchemaFile); | ||
const schema = module[schemaName + "_Schema"]; | ||
if (!schema) { | ||
throw new Error(`module must export a Schema with name ${schemaName}_Schema in ${generatedTypescriptSource}`); | ||
} | ||
if (!schema) { | ||
throw new Error(`module must export a Schema with name ${schemaName}_Schema in ${generatedTypescriptSource}`); | ||
} | ||
debugLog(" generated_source_is_outdated ", schemaName, " to ", generatedTypescriptSource); | ||
if (exports.verbose) { | ||
console.log(" generating ", schemaName, " in ", generatedTypescriptSource); | ||
debugLog(" generated_source_is_outdated ", schemaName, " to ", generatedTypescriptSource); | ||
if (exports.verbose) { | ||
console.log(" generating ", schemaName, " in ", generatedTypescriptSource); | ||
} | ||
const localSchemaFile1 = path.join("../schemas", schemaName + "_schema"); | ||
produce_tscript_code(schema, localSchemaFile1, generatedTypescriptSource); | ||
} | ||
const localSchemaFile1 = path.join("../schemas", schemaName + "_schema"); | ||
produce_tscript_code(schema, localSchemaFile1, generatedTypescriptSource); | ||
} | ||
} | ||
export async function generateTypeScriptCodeFromSchema(schemaName: string) { | ||
const currentFolder = process.cwd(); | ||
const schemafilename = path.join(currentFolder, "schemas", schemaName + "_schema.ts"); | ||
const generatedCodeFolder = path.join(process.cwd(), "_generated_"); | ||
await generateCode(schemaName, schemafilename, generatedCodeFolder); | ||
const currentFolder = process.cwd(); | ||
const schemafilename = path.join(currentFolder, "schemas", schemaName + "_schema.ts"); | ||
const generatedCodeFolder = path.join(process.cwd(), "_generated_"); | ||
await generateCode(schemaName, schemafilename, generatedCodeFolder); | ||
} | ||
@@ -156,40 +161,41 @@ | ||
if (!schema.split) { | ||
console.log("error !",schema); | ||
//xx process.exit(1); | ||
} | ||
// we expect <schema>|<hint> | ||
const hintSchema = schema.split("|"); | ||
if (hintSchema.length === 1) { | ||
// no hint provided | ||
const callerFolder = get_caller_source_filename(); | ||
if (!schema.split) { | ||
console.log("error !", schema); | ||
// xx process.exit(1); | ||
} | ||
// we expect <schema>|<hint> | ||
const hintSchema = schema.split("|"); | ||
if (hintSchema.length === 1) { | ||
// no hint provided | ||
const callerFolder = get_caller_source_filename(); | ||
const defaultHint = path.join(path.dirname(callerFolder), "schemas"); | ||
hintSchema.unshift(defaultHint); | ||
generateCodeFolder = generateCodeFolder ? generateCodeFolder : path.join(path.dirname(callerFolder), "_generated_"); | ||
} | ||
const defaultHint = path.join(path.dirname(callerFolder), "schemas"); | ||
hintSchema.unshift(defaultHint); | ||
generateCodeFolder = generateCodeFolder | ||
? generateCodeFolder | ||
: path.join(path.dirname(callerFolder), "_generated_"); | ||
} | ||
const folderHint = hintSchema[0]; | ||
schema = hintSchema[1]; | ||
const folderHint = hintSchema[0]; | ||
schema = hintSchema[1]; | ||
const schemaName = schema + "_Schema"; | ||
const schemaFile = path.join(folderHint, schema + "_schema.ts"); | ||
const module = await import(schemaFile); | ||
if (!module) { | ||
throw new Error("cannot find " + schemaFile); | ||
} | ||
const schemaObj = module[schemaName]; | ||
const schemaName = schema + "_Schema"; | ||
const schemaFile = path.join(folderHint, schema + "_schema.ts"); | ||
const module = await import(schemaFile); | ||
if (!module) { | ||
throw new Error("cannot find " + schemaFile); | ||
} | ||
const schemaObj = module[schemaName]; | ||
await generateCode(schemaName, schemaFile, generateCodeFolder); | ||
await generateCode(schemaName, schemaFile, generateCodeFolder); | ||
return null; | ||
return null; | ||
} | ||
export function unregisterObject(schema: any, folder: string) { | ||
const generateTypeScriptSource = get_class_tscript_filename(schema.name, folder); | ||
if (fs.existsSync(generateTypeScriptSource)) { | ||
fs.unlinkSync(generateTypeScriptSource); | ||
assert(!fs.existsSync(generateTypeScriptSource)); | ||
} | ||
const generateTypeScriptSource = get_class_tscript_filename(schema.name, folder); | ||
if (fs.existsSync(generateTypeScriptSource)) { | ||
fs.unlinkSync(generateTypeScriptSource); | ||
assert(!fs.existsSync(generateTypeScriptSource)); | ||
} | ||
} | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
171286
2801
Updatedprettier@^1.15.3
Updatedtypescript@^3.2.1