@nx-plus/docusaurus
Version:
Docusaurus plugin for Nx
113 lines (112 loc) • 4.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.applicationSchematic = exports.applicationGenerator = void 0;
const tslib_1 = require("tslib");
const devkit_1 = require("@nrwl/devkit");
const run_tasks_in_serial_1 = require("@nrwl/workspace/src/utilities/run-tasks-in-serial");
const path = require("path");
function normalizeOptions(host, options) {
const name = (0, devkit_1.names)(options.name).fileName;
const projectDirectory = options.directory
? `${(0, devkit_1.names)(options.directory).fileName}/${name}`
: name;
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const projectRoot = `${(0, devkit_1.getWorkspaceLayout)(host).appsDir}/${projectDirectory}`;
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];
return Object.assign(Object.assign({}, options), { name,
projectName,
projectRoot,
projectDirectory,
parsedTags });
}
function addFiles(host, options) {
const templateOptions = Object.assign(Object.assign(Object.assign({}, options), (0, devkit_1.names)(options.name)), { offsetFromRoot: (0, devkit_1.offsetFromRoot)(options.projectRoot), template: '' });
(0, devkit_1.generateFiles)(host, path.join(__dirname, 'files'), options.projectRoot, templateOptions);
}
function updateGitIgnore(host) {
var _a, _b;
const gitIgnorePath = '.gitignore';
if (!host.exists(gitIgnorePath))
return;
const gitIgnoreSource = (_b = (_a = host.read(gitIgnorePath, 'utf-8')) === null || _a === void 0 ? void 0 : _a.trimRight()) !== null && _b !== void 0 ? _b : '';
const ignorePatterns = ['.docusaurus/', '.cache-loader/'].filter((ip) => !gitIgnoreSource.includes(ip));
if (!ignorePatterns.length)
return;
const updatedGitIgnore = (0, devkit_1.applyChangesToString)(gitIgnoreSource, [
{
type: devkit_1.ChangeType.Insert,
index: gitIgnoreSource.length,
text: `
# Generated Docusaurus files
${ignorePatterns.join('\n')}`,
},
]);
host.write(gitIgnorePath, updatedGitIgnore);
}
function updatePrettierIgnore(host) {
var _a, _b;
const prettierIgnorePath = '.prettierignore';
if (!host.exists(prettierIgnorePath))
return;
const prettierIgnoreSource = (_b = (_a = host.read(prettierIgnorePath, 'utf-8')) === null || _a === void 0 ? void 0 : _a.trimRight()) !== null && _b !== void 0 ? _b : '';
const ignorePattern = '.docusaurus/';
if (prettierIgnoreSource.includes(ignorePattern))
return;
const updatedPrettierIgnore = (0, devkit_1.applyChangesToString)(prettierIgnorePath, [
{
type: devkit_1.ChangeType.Insert,
index: prettierIgnoreSource.length,
text: `\n${ignorePattern}`,
},
]);
host.write(prettierIgnorePath, updatedPrettierIgnore);
}
function applicationGenerator(host, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const normalizedOptions = normalizeOptions(host, options);
(0, devkit_1.addProjectConfiguration)(host, normalizedOptions.projectName, {
root: normalizedOptions.projectRoot,
projectType: 'application',
sourceRoot: `${normalizedOptions.projectRoot}/src`,
targets: {
build: {
executor: '@nx-plus/docusaurus:browser',
options: {
outputPath: `dist/${normalizedOptions.projectRoot}`,
},
},
serve: {
executor: '@nx-plus/docusaurus:dev-server',
options: {
port: 3000,
},
},
},
tags: normalizedOptions.parsedTags,
});
addFiles(host, normalizedOptions);
updateGitIgnore(host);
updatePrettierIgnore(host);
const installTask = (0, devkit_1.addDependenciesToPackageJson)(host, {
'@docusaurus/core': '2.1.0',
'@docusaurus/preset-classic': '2.1.0',
'@mdx-js/react': '^1.6.22',
clsx: '^1.2.1',
'prism-react-renderer': '^1.3.5',
react: '^17.0.2',
'react-dom': '^17.0.2',
}, {
'@docusaurus/module-type-aliases': '2.1.0',
typescript: '^4.7.4',
});
if (!normalizedOptions.skipFormat) {
yield (0, devkit_1.formatFiles)(host);
}
return (0, run_tasks_in_serial_1.runTasksInSerial)(installTask);
});
}
exports.applicationGenerator = applicationGenerator;
exports.applicationSchematic = (0, devkit_1.convertNxGenerator)(applicationGenerator);
//# sourceMappingURL=generator.js.map