@k11r/nx-cloudflare-wrangler
Version:
Nx Plugin for Cloudflare Wrangler
65 lines • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = projectGenerator;
const tslib_1 = require("tslib");
const devkit_1 = require("@nx/devkit");
const node_1 = require("@nx/node");
function projectGenerator(tree, schema) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
yield (0, node_1.applicationGenerator)(tree, schema);
const appName = schema.name;
const projectConfiguration = (0, devkit_1.readProjectConfiguration)(tree, appName);
const projectRoot = projectConfiguration.root;
const templatePath = (0, devkit_1.joinPathFragments)(__dirname, './files');
const substitutions = Object.assign({ tmpl: '', zone_id: (_a = schema.zone_id) !== null && _a !== void 0 ? _a : '', account_id: (_b = schema.account_id) !== null && _b !== void 0 ? _b : '', route: (_c = schema.route) !== null && _c !== void 0 ? _c : '', workers_dev: (_d = schema.route) !== null && _d !== void 0 ? _d : true, compatibility_date: new Date().toISOString().split('T')[0] }, (0, devkit_1.names)(schema.name));
// remove all files that were created except for the config files
tree.listChanges()
.filter((fileChange) => fileChange.type === 'CREATE' &&
!fileChange.path.endsWith('/project.json') &&
!fileChange.path.endsWith('.eslintrc.json') &&
fileChange.path !== 'workspace.json')
.forEach((fileChange) => {
tree.delete(fileChange.path);
});
(0, devkit_1.generateFiles)(tree, templatePath, projectRoot, substitutions);
yield (0, devkit_1.formatFiles)(tree);
updateGitIgnore(tree);
addTargets(tree, schema.name);
return () => {
(0, devkit_1.installPackagesTask)(tree);
};
});
}
function addTargets(tree, appName) {
var _a;
try {
const projectConfiguration = (0, devkit_1.readProjectConfiguration)(tree, appName);
const packageRoot = projectConfiguration.root;
const packageSourceRoot = projectConfiguration.sourceRoot;
projectConfiguration.targets = Object.assign(Object.assign({}, ((_a = projectConfiguration.targets) !== null && _a !== void 0 ? _a : {})), { serve: {
executor: '@k11r/nx-cloudflare-wrangler:serve-worker',
}, deploy: {
executor: '@k11r/nx-cloudflare-wrangler:deploy-worker',
} });
(0, devkit_1.updateProjectConfiguration)(tree, appName, projectConfiguration);
}
catch (e) {
console.error(e);
}
}
function updateGitIgnore(tree) {
const requiredIgnores = ['.dist'];
try {
const content = tree.exists('.gitignore')
? tree.read('.gitignore').toString()
: ``;
const ignores = content.split(`\n`);
const ignoresToAdd = requiredIgnores.filter((newIgnore) => !ignores.find((i) => i.trim() === newIgnore.trim()));
tree.write('.gitignore', content + `\n` + ignoresToAdd.join(`\n`));
}
catch (e) {
console.error(e);
}
}
//# sourceMappingURL=index.js.map