@o3r/core
Version:
Core of the Otter Framework
78 lines • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ngGenerateEntitySyncStore = void 0;
const path = require("node:path");
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@o3r/schematics");
/**
* Create an Otter friendly entity sync store
* @param options
*/
function ngGenerateEntitySyncStoreFn(options) {
const generateFiles = (tree, context) => {
const destination = (0, schematics_2.getDestinationPath)('@o3r/core:store', options.path, tree, options.projectName);
const commonTemplates = (0, schematics_1.url)('../common/templates');
const syncEntityTemplates = (0, schematics_1.url)('./templates');
options.storeName = options.storeName?.trim();
options.modelName = options.modelName?.trim();
options.sdkPackage = options.sdkPackage?.trim();
options.modelIdPropName = options.modelIdPropName?.trim();
// Add extra formatted properties
const formattedProperties = {
isAsync: false,
isEntity: true,
storeName: core_1.strings.classify(options.storeName),
cStoreName: core_1.strings.camelize(options.storeName),
scuStoreName: core_1.strings.underscore(options.storeName).toUpperCase(),
hasSDK: !!options.sdkPackage,
hasCustomId: !!options.modelIdPropName && options.modelIdPropName !== 'id',
storeModelName: options.sdkPackage ? `${core_1.strings.classify(options.modelName)}Model` : `${core_1.strings.classify(options.storeName)}Model`,
payloadModelName: options.sdkPackage ? options.modelName : `${core_1.strings.classify(options.storeName)}Model`,
modelIdPropName: options.modelIdPropName || 'id',
reviverModelName: `revive${options.modelName}`,
fileName: core_1.strings.dasherize(options.storeName)
};
let currentStoreIndex = '';
const barrelPath = path.join(destination, options.storeName, 'index.ts');
if (tree.exists(barrelPath)) {
const currentStoreIndexBuffer = tree.read(barrelPath);
currentStoreIndex = currentStoreIndexBuffer ? currentStoreIndexBuffer.toString() : '';
tree.delete(barrelPath);
}
const rules = [];
rules.push((0, schematics_1.mergeWith)((0, schematics_1.apply)(commonTemplates, [
(0, schematics_1.template)({
...core_1.strings,
...options,
...formattedProperties,
currentStoreIndex
}),
(0, schematics_1.renameTemplateFiles)(),
(0, schematics_1.move)(`${destination}/${core_1.strings.dasherize(options.storeName)}`)
]), schematics_1.MergeStrategy.Overwrite), (0, schematics_1.mergeWith)((0, schematics_1.apply)(syncEntityTemplates, [
(0, schematics_1.template)({
...core_1.strings,
...options,
...formattedProperties,
currentStoreIndex
}),
(0, schematics_1.renameTemplateFiles)(),
(0, schematics_1.move)(`${destination}/${core_1.strings.dasherize(options.storeName)}`)
]), schematics_1.MergeStrategy.Overwrite));
if ((0, schematics_2.moduleHasSubEntryPoints)(tree, destination)) {
(0, schematics_2.writeSubEntryPointPackageJson)(tree, destination, core_1.strings.dasherize(options.storeName));
}
return (0, schematics_1.chain)(rules)(tree, context);
};
return (0, schematics_1.chain)([
generateFiles,
options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEsLintFix)()
]);
}
/**
* Create an Otter friendly entity sync store
* @param options
*/
exports.ngGenerateEntitySyncStore = (0, schematics_2.createOtterSchematic)(ngGenerateEntitySyncStoreFn);
//# sourceMappingURL=index.js.map