compass-docgen
Version:
This is a package that will help you to create your documentation for yours components
182 lines (176 loc) • 7.75 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Documentation = void 0;
const core_1 = require("@compass-docgen/core");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const esbuild_1 = __importDefault(require("esbuild"));
const core_2 = require("@compass-docgen/core");
class Documentation {
tsFileDirectory = path.dirname(__filename);
outDir = './build';
config = core_2.DEF_CONFIG;
loading;
constructor() {
this.loading = new Promise(async (resolve) => {
this.config = await core_2.AppConfig.read();
resolve(true);
});
}
async baseReactFiles() {
const sourceDir = path.resolve(this.tsFileDirectory, '../../public');
let targetDir = './' + this.outDir;
if (sourceDir.indexOf('node_modules') > -1) {
targetDir = sourceDir.split('node_modules')[0] + '/' + this.outDir;
}
const files = fs.readdirSync(sourceDir);
var buildPromises = [];
files.forEach((file) => {
const sourceFilePath = path.join(sourceDir, file);
const targetFilePath = path.join(targetDir, file);
buildPromises.push(fs.promises.copyFile(sourceFilePath, targetFilePath));
});
buildPromises.push(fs.promises.writeFile(targetDir + '/README.md', `# Compass
Compass is an open-source project designed to automate the generation of documentation for React components. It offers a convenient way to create a Single [Single Repository](https://www.accenture.com/us-en/blogs/software-engineering-blog/how-to-choose-between-mono-repo-and-poly-repo) for your components, streamlining your development workflow. While currently supporting TypeScript, Compass's roadmap includes plans to incorporate JavaScript support in the future.
This is a prod server
## Quikstart
Install all dependencies
npm install
Start application
npm run dev
`));
await Promise.all(buildPromises);
var indexFile = targetDir + '/index.html';
let indexFileHTML = fs.readFileSync(indexFile, 'utf8');
indexFileHTML = indexFileHTML
.replace('</body>', ` <script src="%PUBLIC_URL%/index.js"></script>
</body>`)
.replace('</head>', `<link rel="stylesheet" href="%PUBLIC_URL%/index.css">
</head>`)
.replace('%TITLE%', this.config.name || 'Docamte')
.replace(new RegExp('%PUBLIC_URL%', 'g'), '');
fs.writeFileSync(indexFile, indexFileHTML);
if (this.config.favicon) {
fs.copyFileSync(path.join(sourceDir, this.config.favicon), path.join(targetDir, 'favicon.ico'));
}
}
async build() {
await this.loading;
var indexFile = path.resolve(this.tsFileDirectory + '../../../index.tsx');
console.warn(indexFile);
const clientEnv = {
'process.env.NODE_ENV': JSON.stringify('production'),
'process.env.PORT': JSON.stringify(this.config.port),
};
await esbuild_1.default
.build({
entryPoints: [indexFile],
bundle: true,
minify: true,
sourcemap: true,
define: clientEnv,
outdir: this.outDir,
publicPath: '/public',
loader: {
'.tsx': 'tsx',
'.ts': 'ts',
'.js': 'jsx',
'.scss': 'css',
'.png': 'dataurl',
'.jpg': 'dataurl',
'.svg': 'dataurl',
},
jsxFactory: 'React.createElement',
})
.catch((error) => {
console.log(error);
process.exit(1);
});
await Promise.all([
this.dependences(),
this.baseReactFiles(),
this.createPackageJson(),
]).then(() => {
console.log('Documentation generated');
});
}
async createPackageJson() {
const PACKAGE = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
let version = PACKAGE.devDependencies['compass-docgen'];
if (PACKAGE.name == 'compass-docgen') {
version = PACKAGE.version;
}
const packageJson = {
name: this.config.name.replace(/ /g, '-').toLowerCase() + '-doc',
version: '1.0.0',
description: 'Server to run the documentation',
scripts: {
start: 'compass-server start',
production: 'compass-server start',
},
dependencies: {
'@compass-docgen/server': 'latest',
},
};
fs.promises.writeFile(path.resolve(this.outDir, 'package.json'), JSON.stringify(packageJson));
}
async copyDocFile(currFile) {
const DESTINATION = path.resolve(this.outDir, currFile);
await fs.promises.mkdir(path.dirname(DESTINATION), {
recursive: true,
});
await fs.promises.copyFile(currFile, DESTINATION);
}
async dependences() {
await this.loading;
var componentsService = new core_1.ComponentService();
var components = await componentsService.getComponents(this.config.dir);
var exportCommands = '';
const DEP_DIR = './../../app/pages/component/live-editor/component-dependences.ts';
var promissesOfCopyFiles = [];
for (var componentName in components) {
var component = components[componentName];
const WORK_DIR = path.relative(path.resolve(__dirname, path.dirname(DEP_DIR)), path.dirname(component.fullPath));
exportCommands += `export { ${component.name} } from '${WORK_DIR.replace(/\\/g, '/')}/${path.parse(path.basename(component.fullPath)).name}'; \n`;
// exportCommands += `var ${
// component.name
// }_1 = require('${WORK_DIR.replace(/\\/g, '/')}/${
// path.parse(path.basename(component.fullPath)).name
// }');\n
// Object.defineProperty(exports, ${componentName}, { enumerable: true, get: function () { return ${
// component.name
// }_1.${componentName}; } });`;
promissesOfCopyFiles.push(this.copyDocFile(component.docPath));
}
promissesOfCopyFiles.push(fs.promises.writeFile(path.resolve(this.tsFileDirectory, DEP_DIR), exportCommands));
await Promise.all(promissesOfCopyFiles);
return components;
}
}
exports.Documentation = Documentation;