@gasket/plugin-intl
Version:
NodeJS script to build localization files.
68 lines (67 loc) • 2.1 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
getPackageDirs: function() {
return getPackageDirs;
},
packageName: function() {
return packageName;
},
saveJsonFile: function() {
return saveJsonFile;
}
});
const _fsextra = /*#__PURE__*/ _interop_require_default(require("fs-extra"));
const _path = /*#__PURE__*/ _interop_require_default(require("path"));
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
/**
* Check if path has package.json file and return the name.
* @param {string} targetDir - File path
* @returns {Promise<string|undefined>} result
*/ async function packageName(targetDir) {
try {
const pkg = await _fsextra.default.readJson(_path.default.join(targetDir, 'package.json'));
return pkg.name;
} catch {
// skip;
}
}
/**
* Find all directories under target dir, recursively
* @type {import('../internal.d.ts').getPackageDirs}
*/ async function* getPackageDirs(parentDir, dirList = []) {
const files = await _fsextra.default.readdir(parentDir);
for (const file of files){
const filePath = _path.default.join(parentDir, file);
if ((await _fsextra.default.stat(filePath)).isDirectory()) {
const pkgName = await packageName(filePath);
if (pkgName) {
yield [
pkgName,
filePath
];
} else {
yield* getPackageDirs(_path.default.join(filePath, '/'), dirList);
}
}
}
}
/**
* Saves a json file to disk
* @param {string} filePath - Path to the target file
* @param {JSON} json - JSON to write out
* @returns {Promise} promise
*/ async function saveJsonFile(filePath, json) {
return await _fsextra.default.writeFile(filePath, JSON.stringify(json, null, 2), 'utf-8');
}