ilib-assemble
Advanced tools
Comparing version
149
package.json
{ | ||
"name": "ilib-assemble", | ||
"version": "1.2.1", | ||
"main": "./src/index.js", | ||
"type": "module", | ||
"bin": { | ||
"ilib-assemble": "./src/index.js", | ||
"ilib-assemble.bat": "./ilib-assemble.bat" | ||
"name": "ilib-assemble", | ||
"version": "1.3.1", | ||
"main": "./src/index.js", | ||
"type": "module", | ||
"bin": { | ||
"ilib-assemble": "./src/index.js", | ||
"ilib-assemble.bat": "./ilib-assemble.bat" | ||
}, | ||
"description": "Utility to assemble locale data and resource files into single files for webpack to include", | ||
"keywords": [ | ||
"internationalization", | ||
"i18n", | ||
"localization", | ||
"l10n", | ||
"globalization", | ||
"g11n", | ||
"data", | ||
"locale" | ||
], | ||
"homepage": "https://github.com/iLib-js/ilib-mono/packages/ilib-assemble", | ||
"bugs": "https://github.com/iLib-js/ilib-mono/issues", | ||
"email": "ehoogerbeets@gmail.com", | ||
"license": "Apache-2.0", | ||
"author": { | ||
"name": "Edwin Hoogerbeets", | ||
"web": "http://www.translationcircle.com/", | ||
"email": "ehoogerbeets@gmail.com" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Edwin Hoogerbeets", | ||
"email": "ehoogerbeets@gmail.com" | ||
}, | ||
"description": "Utility to assemble locale data and resource files into single files for webpack to include", | ||
"keywords": [ | ||
"internationalization", | ||
"i18n", | ||
"localization", | ||
"l10n", | ||
"globalization", | ||
"g11n", | ||
"data", | ||
"locale" | ||
], | ||
"homepage": "https://github.com/iLib-js/ilib-assemble", | ||
"bugs": "https://github.com/iLib-js/ilib-assemble/issues", | ||
"email": "ehoogerbeets@gmail.com", | ||
"license": "Apache-2.0", | ||
"author": { | ||
"name": "Edwin Hoogerbeets", | ||
"web": "http://www.translationcircle.com/", | ||
"email": "ehoogerbeets@gmail.com" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Edwin Hoogerbeets", | ||
"email": "ehoogerbeets@gmail.com" | ||
}, | ||
{ | ||
"name": "Goun Lee", | ||
"email": "goun.lee@lge.com" | ||
} | ||
], | ||
"files": [ | ||
"src", | ||
"assemble.bat", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:iLib-js/ilib-assemble.git" | ||
}, | ||
"scripts": { | ||
"dist": "npm pack", | ||
"test": "npm-run-all test:cli", | ||
"test:cli": "LANG=en_US.UTF8 node --experimental-vm-modules node_modules/.bin/jest --testEnvironment node", | ||
"test:watch": "LANG=en_US.UTF8 node --experimental-vm-modules node_modules/.bin/jest --testEnvironment node --watch", | ||
"debug": "node --experimental-vm-modules --inspect-brk node_modules/.bin/jest --testEnvironment node -i", | ||
"clean": "git clean -f -d src test ; rm -rf lib *.tgz", | ||
"doc": "mkdir -p docs ; jsdoc2md -c jsdoc.json --separators --source src/*.js -m table > docs/ilibAssemble.md ; npm run doc:html", | ||
"doc:html": "jsdoc -c jsdoc.json" | ||
}, | ||
"engines": { | ||
"node": ">=16.0" | ||
}, | ||
"devDependencies": { | ||
"docdash": "^2.0.2", | ||
"ilib-mock": "file:test/ilib-mock/ilib-mock-1.0.0.tgz", | ||
"jest": "^29.7.0", | ||
"jsdoc": "^4.0.2", | ||
"jsdoc-to-markdown": "^8.0.0", | ||
"npm-run-all": "^4.1.5" | ||
}, | ||
"dependencies": { | ||
"ilib-common": "^1.1.3", | ||
"ilib-locale": "^1.2.2", | ||
"json5": "^2.2.3", | ||
"mkdirp": "^3.0.1", | ||
"options-parser": "^0.4.0" | ||
{ | ||
"name": "Goun Lee", | ||
"email": "goun.lee@lge.com" | ||
} | ||
} | ||
], | ||
"files": [ | ||
"src", | ||
"ilib-assemble.bat", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:iLib-js/ilib-mono.git" | ||
}, | ||
"engines": { | ||
"node": ">=16.0" | ||
}, | ||
"devDependencies": { | ||
"docdash": "^2.0.2", | ||
"ilib-mock": "file:test/ilib-mock/ilib-mock-1.0.0.tgz", | ||
"jest": "^29.7.0", | ||
"jsdoc": "^4.0.2", | ||
"jsdoc-to-markdown": "^8.0.0", | ||
"npm-run-all": "^4.1.5" | ||
}, | ||
"dependencies": { | ||
"ilib-locale": "^1.2.2", | ||
"json5": "^2.2.3", | ||
"options-parser": "^0.4.0", | ||
"uglify-js-export": "^3.19.2", | ||
"ilib-common": "^1.1.5" | ||
}, | ||
"scripts": { | ||
"test": "pnpm test:cli", | ||
"test:cli": "LANG=en_US.UTF8 NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --testEnvironment node", | ||
"test:watch": "LANG=en_US.UTF8 NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --testEnvironment node --watch", | ||
"debug": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --inspect-brk\" jest --testEnvironment node -i", | ||
"clean": "git clean -f -d src test ; rm -rf lib *.tgz", | ||
"doc": "mkdir -p docs && jsdoc2md -c jsdoc.json --separators --source src/*.js -m table > docs/ilibAssemble.md && pnpm doc:html", | ||
"doc:html": "jsdoc -c jsdoc.json" | ||
} | ||
} |
@@ -49,2 +49,6 @@ # ilib-assemble | ||
(Update) Since the version v1.3.0, this tool has a ability to assemble the monolithic "ilib" package. | ||
We decided to call it as 'legacy' version ilib. It works to use legacyilib flag. | ||
Some of additional options are added to support it. Please refer to the option description of usage ("ilibPath", "ilibincPath", "outjsFileName"). | ||
## Basic Operation | ||
@@ -97,2 +101,17 @@ | ||
## Basic Operation for legacy version of ilib | ||
1. Write the `inc` file to specify which files to assemble. | ||
If not, the default file `./src/ilib-all-inc.js` will be used which assembles all ilib classes and data together. | ||
If you only need a subset of ilib, copy this file and delete the parts you do not need. | ||
2. Read the javscript files from the inc file list and extract the list of dependent JS and locale data. | ||
3. After the tool has finished executing, the two types of files (code, data) will be placed the `output-dir`. | ||
One is for the result of all JS files merged into a single usable file. If the file name is not specified, the default file name is `ilib-all.js`. and another file is for the locale data per `[locale].js`. | ||
The data is written as follows: `ilib.data_[feature]_locale = { [data] }` | ||
Here is an example of localeinfo for the ko-KR locale. | ||
```js | ||
ilib.data.localeinfo_ko = { [data] } | ||
ilib.data.localeinfo_ko_KR = { [data] } | ||
ilib.data.localeinfo_und_KR = { [data] } | ||
``` | ||
## Usage | ||
@@ -130,2 +149,10 @@ | ||
this option multiple times, once for each resources directory. | ||
* -legacyilib. The flag to indicate assemble the legacy version of ilib. | ||
* --ilibPath or -i. Specify the location where the legacy version of ilib is installed. | ||
* --ilibincPath or -f. Specify name of javascript file to process. If not given, | ||
the default (`./src/ilib-all-inc.js`)file will be used. | ||
* --outjsFileName or n. Specify the resulting assembled output file name. | ||
The default is `ilib-all.js` | ||
* --customLocalePath or p. Specify the path to customized locale data that | ||
overrides existing open-source locale data. | ||
@@ -181,3 +208,3 @@ The output-dir is required and specifies the directory where the output is | ||
Copyright © 2022, JEDLSoft | ||
Copyright © 2022, 2024 JEDLSoft | ||
@@ -199,32 +226,2 @@ Licensed under the Apache License, Version 2.0 (the "License"); | ||
### v1.2.0 | ||
- added --resources option to include translated resource files into the | ||
output bundle | ||
- fixed missing dependencies | ||
### v1.1.2 | ||
- work with the hybrid commonjs/ESM modules | ||
### v1.1.1 | ||
- Updated dependencies to avoid the polyfill bloat | ||
### v1.1.0 | ||
- added the ability to add a nodule to the list of modules to include the locale | ||
data for. This is often used to include the locale data of the current | ||
package for testing. | ||
- added the ability to get the list of locales to process from a json file | ||
- locale data in js files now get output as a function that returns the data. That | ||
way you don't need any babel plugins to process exported const values. | ||
(ie. you don't need babel-plugin-transform-add-module-exports) | ||
- only resolve paths to modules when they are module references. That is, they are | ||
not relative to the current dir nor are they absolute paths | ||
### v1.0.0 | ||
- initial version | ||
- Code to replace the ilib-webpack-loader and ilib-webpack-plugin by | ||
performing the same task, but outside of webpack. | ||
See [CHANGELOG.md](./CHANGELOG.md) |
244
src/index.js
@@ -7,3 +7,3 @@ #!/usr/bin/env node | ||
* | ||
* Copyright © 2022 JEDLSoft | ||
* Copyright © 2022, 2024 JEDLSoft | ||
* | ||
@@ -29,3 +29,2 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
import path from 'path'; | ||
import mkdirp from 'mkdirp'; | ||
import json5 from 'json5'; | ||
@@ -37,2 +36,3 @@ | ||
import scanResources from './scanres.js'; | ||
import assembleilib from './legacyilibassemble.js'; | ||
@@ -81,2 +81,26 @@ const optionConfig = { | ||
help: "Include translated resource files in the output files such that they can be loaded with ilib-resbundle. The resource files should come from ilib's loctool or other such localization tool which produces a set of translated resource files. VAL is the path to the root of a resource file tree." | ||
}, | ||
legacyilib: { | ||
short: "o", | ||
flag: true, | ||
help: "Assemble data for the legacy version of ilib." | ||
}, | ||
ilibPath: { | ||
short: "i", | ||
help: "Specify the location where the legacy version of ilib is installed." | ||
}, | ||
ilibincPath: { | ||
short: "f", | ||
"default": "./src/ilib-all-inc.js", | ||
help: "Specify the name of the Javascript inc file to process for legacy ilib assembly." | ||
}, | ||
outjsFileName: { | ||
short: "n", | ||
"default": "ilib-all.js", | ||
help: "Specify the resulting assembled output file name for legacy ilib assembly." | ||
}, | ||
customLocalePath: { | ||
short: "p", | ||
"default" : undefined, | ||
help: "Specify the path to customized locale data that overrides existing open-source locale data." | ||
} | ||
@@ -96,3 +120,3 @@ }; | ||
if (!options.opt.quiet) console.log("ilib-assemble - Copyright (c) 2022 JEDLsoft, All rights reserved."); | ||
if (!options.opt.quiet) console.log("ilib-assemble - Copyright (c) 2022, 2024 JEDLsoft, All rights reserved."); | ||
@@ -107,3 +131,3 @@ const outputPath = options.args[0]; | ||
// file not found, so let's make it | ||
mkdirp(outputPath); | ||
fs.mkdirSync(outputPath, { recursive: true }); | ||
if (!options.opt.quiet) console.log(`Created output path: ${outputPath}`); | ||
@@ -119,7 +143,2 @@ } else if (stat.errno) { | ||
let paths = options.args.slice(1); | ||
if (paths.length === 0) { | ||
paths.push("."); | ||
} | ||
if (options.opt.localefile) { | ||
@@ -139,68 +158,53 @@ const json = json5.parse(fs.readFileSync(options.opt.localefile, "utf-8")); | ||
}); | ||
if (!options.opt.format) { | ||
options.opt.format = "js"; | ||
} | ||
if (!options.opt.quiet) console.log(`Assembling data for locales: ${options.opt.locales.join(", ")}`); | ||
if (!options.opt.legacyilib) { | ||
let paths = options.args.slice(1); | ||
if (paths.length === 0) { | ||
paths.push("."); | ||
} | ||
if (!options.opt.quiet) console.log(`\n\nScanning input paths: ${JSON.stringify(paths)}`); | ||
if (!options.opt.format) { | ||
options.opt.format = "js"; | ||
} | ||
let files = []; | ||
if (!options.opt.quiet) console.log(`Assembling data for locales: ${options.opt.locales.join(", ")}`); | ||
paths.forEach((pathName) => { | ||
if (!options.opt.quiet) console.log(` Scanning ${pathName} for Javascript files...`); | ||
files = files.concat(walk(pathName, options.opt)); | ||
}); | ||
if (!options.opt.quiet) console.log(`\n\nScanning input paths: ${JSON.stringify(paths)}`); | ||
let ilibModules = new Set(); | ||
let files = []; | ||
if (!options.opt.quiet) console.log(`\n\nScanning javascript files...`); | ||
paths.forEach((pathName) => { | ||
if (!options.opt.quiet) console.log(` Scanning ${pathName} for Javascript files...`); | ||
files = files.concat(walk(pathName, options.opt)); | ||
}); | ||
files.forEach((file) => { | ||
if (!options.opt.quiet) console.log(` ${file} ...`); | ||
scan(file, ilibModules); | ||
}); | ||
let ilibModules = new Set(); | ||
if (options.opt.module) { | ||
options.opt.module.forEach(module => { | ||
if (!options.opt.quiet) console.log(`\nAdding module ${module} to the list of modules to search`); | ||
ilibModules.add((module[0] === '.') ? path.join(process.cwd(), module) : module); | ||
if (!options.opt.quiet) console.log(`\n\nScanning javascript files...`); | ||
files.forEach((file) => { | ||
if (!options.opt.quiet) console.log(` ${file} ...`); | ||
scan(file, ilibModules); | ||
}); | ||
} | ||
let localeData = {}; | ||
if (!options.opt.quiet) console.log(`\n\nScanning ilib modules for locale data`); | ||
let promise = Promise.resolve(false); | ||
ilibModules.forEach((module) => { | ||
if (!options.opt.quiet) console.log(` Scanning module ${module} ...`); | ||
promise = promise.then(result => { | ||
return scanModule(module, options.opt).then(data => { | ||
if (data && typeof(data) === "object") { | ||
// merge in the sublocales separately | ||
for (const sublocale in data) { | ||
localeData = JSUtils.merge(localeData, data[sublocale], true); | ||
} | ||
return true; | ||
} | ||
return result; | ||
if (options.opt.module) { | ||
options.opt.module.forEach(module => { | ||
if (!options.opt.quiet) console.log(`\nAdding module ${module} to the list of modules to search`); | ||
ilibModules.add((module[0] === '.') ? path.join(process.cwd(), module) : module); | ||
}); | ||
}); | ||
}); | ||
} | ||
let localeData = {}; | ||
if (!options.opt.quiet) { | ||
promise.then(result => { | ||
console.log(`\n\nScanning directories for resource files`); | ||
}); | ||
} | ||
if (options.opt.resources) { | ||
options.opt.resources.forEach(resDir => { | ||
if (!options.opt.quiet) console.log(`\n\nScanning ilib modules for locale data`); | ||
let promise = Promise.resolve(false); | ||
ilibModules.forEach((module) => { | ||
if (!options.opt.quiet) console.log(` Scanning module ${module} ...`); | ||
promise = promise.then(result => { | ||
if (!options.opt.quiet) console.log(` ${resDir} ...`); | ||
return scanResources(resDir, options).then(data => { | ||
// console.log(`Received data ${JSON.stringify(data, undefined, 4)}`); | ||
if (data) { | ||
localeData = JSUtils.merge(localeData, data, true); | ||
return scanModule(module, options.opt).then(data => { | ||
if (data && typeof(data) === "object") { | ||
// merge in the sublocales separately | ||
for (const sublocale in data) { | ||
localeData = JSUtils.merge(localeData, data[sublocale], true); | ||
} | ||
return true; | ||
@@ -212,56 +216,82 @@ } | ||
}); | ||
} | ||
const spaces = " "; | ||
function indent(str, howMany) { | ||
return str.split(/\n/g).map(line => { | ||
return spaces.substring(0, howMany*4) + line; | ||
}).join("\n"); | ||
}; | ||
promise.then(result => { | ||
// console.log(`localeData is: ${JSON.stringify(localeData, undefined, 4)}`); | ||
if (!options.opt.quiet) { | ||
promise.then(result => { | ||
console.log(`\n\nScanning directories for resource files`); | ||
}); | ||
} | ||
let hadOutput = false; | ||
if (result) { | ||
if (!options.opt.quiet) console.log("\n\nWriting out data..."); | ||
if (options.opt.resources) { | ||
options.opt.resources.forEach(resDir => { | ||
promise = promise.then(result => { | ||
if (!options.opt.quiet) console.log(` ${resDir} ...`); | ||
return scanResources(resDir, options).then(data => { | ||
// console.log(`Received data ${JSON.stringify(data, undefined, 4)}`); | ||
if (data) { | ||
localeData = JSUtils.merge(localeData, data, true); | ||
return true; | ||
} | ||
return result; | ||
}); | ||
}); | ||
}); | ||
} | ||
for (let i = 0; i < options.opt.locales.length; i++) { | ||
const locale = options.opt.locales[i]; | ||
// assemble all the sublocales into a single file | ||
const sublocales = Utils.getSublocales(locale); | ||
const contents = {}; | ||
sublocales.forEach((sublocale) => { | ||
if (!JSUtils.isEmpty(localeData[sublocale])) { | ||
contents[sublocale] = localeData[sublocale]; | ||
const spaces = " "; | ||
function indent(str, howMany) { | ||
return str.split(/\n/g).map(line => { | ||
return spaces.substring(0, howMany*4) + line; | ||
}).join("\n"); | ||
}; | ||
promise.then(result => { | ||
// console.log(`localeData is: ${JSON.stringify(localeData, undefined, 4)}`); | ||
let hadOutput = false; | ||
if (result) { | ||
if (!options.opt.quiet) console.log("\n\nWriting out data..."); | ||
for (let i = 0; i < options.opt.locales.length; i++) { | ||
const locale = options.opt.locales[i]; | ||
// assemble all the sublocales into a single file | ||
const sublocales = Utils.getSublocales(locale); | ||
const contents = {}; | ||
sublocales.forEach((sublocale) => { | ||
if (!JSUtils.isEmpty(localeData[sublocale])) { | ||
contents[sublocale] = localeData[sublocale]; | ||
} | ||
}); | ||
const outputName = path.join(outputPath, `${locale}.js`); | ||
let contentStr = options.opt.compressed ? | ||
JSON.stringify(contents) : | ||
JSON.stringify(contents, undefined, 4); | ||
switch (options.opt.format) { | ||
case 'js': | ||
contentStr = | ||
"export default function getLocaleData() {\n" + | ||
` return ${indent(contentStr, 1)};\n` + | ||
"};"; | ||
break; | ||
case 'cjs': | ||
contentStr = `module.exports = ${contentStr};`; | ||
break; | ||
} | ||
}); | ||
const outputName = path.join(outputPath, `${locale}.js`); | ||
let contentStr = options.opt.compressed ? | ||
JSON.stringify(contents) : | ||
JSON.stringify(contents, undefined, 4); | ||
switch (options.opt.format) { | ||
case 'js': | ||
contentStr = | ||
"export default function getLocaleData() {\n" + | ||
` return ${indent(contentStr, 1)};\n` + | ||
"};"; | ||
break; | ||
case 'cjs': | ||
contentStr = `module.exports = ${contentStr};`; | ||
break; | ||
if (contentStr.length) { | ||
fs.writeFileSync(outputName, contentStr, "utf-8"); | ||
hadOutput = true; | ||
} | ||
} | ||
if (contentStr.length) { | ||
fs.writeFileSync(outputName, contentStr, "utf-8"); | ||
hadOutput = true; | ||
if (hadOutput) { | ||
if (!options.opt.quiet) console.log(`Done. Output is in ${outputPath}.`); | ||
return true; | ||
} | ||
} | ||
if (hadOutput) { | ||
if (!options.opt.quiet) console.log(`Done. Output is in ${outputPath}.`); | ||
return true; | ||
} | ||
if (!options.opt.quiet) console.log("Done. No locale data found."); | ||
}); | ||
} else { | ||
assembleilib(options); | ||
} | ||
if (!options.opt.quiet) console.log("Done. No locale data found."); | ||
}); | ||
console.log("DONE"); |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
59493
33.94%11
22.22%951
70.43%1
Infinity%223
-1.33%5
25%+ Added
+ Added
- Removed
- Removed
Updated