Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-organize improve #150

Merged
merged 3 commits into from
Nov 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 42 additions & 48 deletions scripts/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const path = require("path");
const chalk = require("chalk");
const globFs = require("glob-fs")();
const outdent = require("outdent");
const { camelCase } = require("lodash");
const { camelCase, startCase } = require("lodash");
const transpileTs = require("./transpile-ts");

// Create a new folder at the specified path
function createDirectory(path) {
try {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
fs.mkdirSync(path, { recursive: true });
}
} catch (err) {
console.error(`[createDirectory]: Failed to create director at ${path}`);
Expand Down Expand Up @@ -42,29 +42,6 @@ function walkSync(dir, filelist = []) {
return filelist;
}

const generateJsFiles = filePath => {
const code = fs.readFileSync(path.join(process.cwd(), filePath), {
encoding: "utf8",
});

const transpiledCode = transpileTs(code);
const templateFilePath = filePath
.replace("stories", `stories${path.sep}__js`)
.replace(".tsx", ".jsx");

const __jsPath = path.dirname(templateFilePath).split("__js")[0];

const templateDir = path.join(process.cwd(), __jsPath, "__js");
createDirectory(templateDir);

const componentName = path.basename(templateFilePath);
const templatePath = path.join(templateDir, componentName);

console.log(chalk.green.bold(`CREATED: ${componentName}`));

createFile(templatePath, transpiledCode);
};

// -> get all the component folders [accordion, breadcrumb...]
// -> find all the .components inside the folders
// -> create object with component pairs
Expand All @@ -83,30 +60,25 @@ function getComponentFolderPairs() {
).filter(path => !path.match(/(__mocks__|utils)/));

const components = componentFolders.reduce((prev, curr) => {
const folderName = path.parse(curr);

const folderName = path.basename(curr);
const allfiles = walkSync(`${curr}${path.sep}stories`).filter(fn =>
fn.match(/(\.component\.tsx|\.css)$/),
);

return { ...prev, [folderName.base]: allfiles };
return { ...prev, [folderName]: allfiles };
}, {});

console.log(
chalk.blueBright.bold(`Component Folder Pairs Successfully Created..`),
chalk.blueBright.bold(`\nComponent Folder Pairs Successfully Created..`),
);

return components;
}

function generateImportString(component, index) {
const componentPath = component
.split("stories")[1]
.replace("\\", "")
.replace(/\\/g, "/");

const componentPath = component.split("stories")[1].replace(/\\/g, "/");
const jsComponentName = componentPath.replace("tsx", "jsx");
const componentName = path.basename(componentPath);
const jsComponentName = componentName.replace("tsx", "jsx");
const templateVarName = camelCase(
componentName.replace(".component.tsx", "").replace(".css", ""),
);
Expand All @@ -119,16 +91,16 @@ function generateImportString(component, index) {
let importString = outdent`
${warningMsg}
// @ts-ignore
export { default as ${templateVarName}Template } from "!!raw-loader!./${componentPath}";\n
export { default as ${templateVarName}Template } from "!!raw-loader!.${componentPath}";\n
// @ts-ignore
export { default as ${templateVarName}TemplateJs } from "!!raw-loader!./__js/${jsComponentName}";\n
export { default as ${templateVarName}TemplateJs } from "!!raw-loader!./__js${jsComponentName}";\n
`;

if (componentPath.endsWith(".css")) {
importString = outdent`
${warningMsg}
// @ts-ignore
export { default as ${templateVarName}CssTemplate } from "!!raw-loader!./${componentPath}";\n
export { default as ${templateVarName}CssTemplate } from "!!raw-loader!.${componentPath}";\n
`;
}

Expand All @@ -140,8 +112,6 @@ function generateTemplateFile() {
console.log(chalk.blueBright.bold(`Generating template.ts files..`));

Object.keys(components).forEach(componentName => {
const componentPairs = components[componentName];

const templateFilePath = path.join(
__dirname,
"../src",
Expand All @@ -152,30 +122,54 @@ function generateTemplateFile() {

createFile(templateFilePath, "");

const componentPairs = components[componentName];
componentPairs.forEach((component, index) => {
const importString = generateImportString(component, index);

console.log(
chalk.red.yellow(
`CREATED template.ts (${chalk.cyanBright(
path.relative("./", templateFilePath),
)})`,
),
);

fs.appendFileSync(templateFilePath, importString, "UTF-8", {
flags: "a+",
});
});

console.log(
chalk.green.bold(
`CREATED: ${startCase(componentName)} templates at ${chalk.cyanBright(
path.relative("./", templateFilePath),
)}`,
),
);
});
}

const generateJsFiles = filePath => {
const code = fs.readFileSync(path.join(process.cwd(), filePath), {
encoding: "utf8",
});

const transpiledCode = transpileTs(code);
const templateFilePath = filePath
.replace("stories", `stories${path.sep}__js`)
.replace(".tsx", ".jsx");
const templateDir = path.join(process.cwd(), path.dirname(templateFilePath));

createDirectory(templateDir);

const componentName = path.basename(templateFilePath);
const templatePath = path.join(templateDir, componentName);

console.log(chalk.green.bold(`CREATED: ${componentName}`));

createFile(templatePath, transpiledCode);
};

function globRecurse(file) {
// `file.pattern` is an object with a `glob` (string) property
file.recurse = file.pattern.glob.indexOf("**") !== -1;
return file;
}

const files = globFs.use(globRecurse).readdirSync("**/*.component.tsx", {});

console.log(chalk.blueBright.bold(`Generating JS Files..`));
files.forEach(filePath => generateJsFiles(filePath));

Expand Down