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

refactor: add rollup + refactor to compose steps + tests #1

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"compilerOptions": {
"sourceMap": true,
"baseUrl": "./src",
"module": "ES6"
}
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"type": "module",
"private": false,
"scripts": {
"build": "rollup --config",
"test": "node --test",
"test:watch": "node --test --watch",
"test:coverage": "node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info",
Expand All @@ -16,5 +17,9 @@
"dependencies": {
"@inquirer/prompts": "^4.3.2",
"commander": "^12.0.0"
},
"devDependencies": {
"@rollup/plugin-json": "^6.1.0",
"rollup": "^4.14.1"
}
}
13 changes: 13 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "rollup";

import json from "@rollup/plugin-json";

export default defineConfig({
input: "src/main.js",
output: {
file: "dist/tricorder.js",
format: "es",
sourcemapFile: true,
},
plugins: [json()],
});
31 changes: 31 additions & 0 deletions src/actions/guided.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@ import {
postfixChoices,
resourcesPaths,
tsTypeChoices,
vueVersionsChoices,
} from "../constants/choices.js";
import { boolAsText } from "../utils/string.js";

import { guidedFlowGenerator } from "../flows/guided-flow-generator.js";
import { FrameworkEnum } from "../enums/frameworks.js";

/**
* @typedef {{
* type: string;
* name: string;
* framework: string;
* version: string | number
* resourcePath: string;
* testPath: string
* storyPath: string
* testPostfix: string;
* typescript: boolean;
* withTest: boolean;
* withStory: boolean;
* }} Answers
*/

export async function guidedAction() {
let withTest = false;
Expand All @@ -19,11 +37,20 @@ export async function guidedAction() {
let storyPath = null;
let resourcePath = null;

let version = null;

const framework = await select({
message: "Select your framework/lib",
choices: frameworksAndLibsChoices,
});

if (framework === FrameworkEnum.Vue) {
version = await select({
message: "What is your Vue version?",
choices: vueVersionsChoices,
});
}

const typescript = await confirm({ message: "Do you use TypeScript?" });

const type = await select({
Expand Down Expand Up @@ -95,8 +122,12 @@ export async function guidedAction() {

const name = await input({ message: "Name" });

/**
* @type {Answers}
*/
const answers = {
framework,
version,
name,
resourcePath,
testPath,
Expand Down
12 changes: 10 additions & 2 deletions src/constants/choices.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { FrameworkEnum } from "../enums/frameworks.js";
import { VueVersionEnum } from "../enums/vue-version.js";

export const tsTypeChoices = [
{ name: "Page", value: "page" },
{ name: "Component", value: "component" },
Expand All @@ -23,8 +26,13 @@ export const jsTypeChoices = [
];

export const frameworksAndLibsChoices = [
{ name: "Vue", value: "vue" },
{ name: "React", value: "react" },
{ name: "Vue", value: FrameworkEnum.Vue },
{ name: "React", value: FrameworkEnum.React },
];

export const vueVersionsChoices = [
{ name: "Vue 2", value: VueVersionEnum[2] },
{ name: "Vue 3", value: VueVersionEnum[3] },
];

export const postfixChoices = [
Expand Down
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./choices.js";
export * from "./templates.js";
31 changes: 31 additions & 0 deletions src/constants/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Built-in templates path dictionary
*/
export const templates = {
react: {
ts: {
functional: "src/templates/react/ts/FuncionalComponent.tsx",
},
js: {
functional: "src/templates/react/js/FuncionalComponent.jsx",
},
},
vue: {
2: {
js: {
options: "src/templates/vue/3/js/OptionsComponent.vue",
},
ts: {
options: "src/templates/vue/2/ts/OptionsComponent.vue",
},
},
3: {
ts: {
setup: "src/templates/vue/3/ts/SetupComponent.vue",
},
js: {
setup: "src/templates/vue/3/js/SetupComponent.vue",
},
},
},
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const FileExtensions = {
export const FileExtensionEnum = {
ts: "ts",
tsx: "tsx",
js: "js",
Expand Down
16 changes: 8 additions & 8 deletions src/enums/file-extension.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { describe, it } from "node:test";
import assert from "node:assert/strict";

import { FileExtensions } from "./file-extensions.js";
import { FileExtensionEnum } from "./file-extension.js";

describe("FileExtension Enum", () => {
describe("FileExtensionEnum", () => {
it("should return properly value", () => {
assert.strictEqual(FileExtensions.js, "js");
assert.strictEqual(FileExtensions.jsx, "jsx");
assert.strictEqual(FileExtensions.json, "json");
assert.strictEqual(FileExtensions.ts, "ts");
assert.strictEqual(FileExtensions.tsx, "tsx");
assert.strictEqual(FileExtensions.vue, "vue");
assert.strictEqual(FileExtensionEnum.js, "js");
assert.strictEqual(FileExtensionEnum.jsx, "jsx");
assert.strictEqual(FileExtensionEnum.json, "json");
assert.strictEqual(FileExtensionEnum.ts, "ts");
assert.strictEqual(FileExtensionEnum.tsx, "tsx");
assert.strictEqual(FileExtensionEnum.vue, "vue");
});
});
4 changes: 4 additions & 0 deletions src/enums/frameworks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const FrameworkEnum = {
Vue: "vue",
React: "react",
};
11 changes: 11 additions & 0 deletions src/enums/frameworks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, it } from "node:test";
import assert from "node:assert/strict";

import { FrameworkEnum } from "./frameworks.js";

describe("FrameworksEnum", () => {
it("should return properly values", () => {
assert.strictEqual(FrameworkEnum.Vue, "vue");
assert.strictEqual(FrameworkEnum.React, "react");
});
});
3 changes: 3 additions & 0 deletions src/enums/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./file-extension.js";
export * from "./frameworks.js";
export * from "./vue-version.js";
4 changes: 4 additions & 0 deletions src/enums/vue-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const VueVersionEnum = {
2: 2,
3: 3,
};
10 changes: 10 additions & 0 deletions src/enums/vue-version.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { VueVersionEnum } from "./vue-version";

describe("FrameworksEnum", () => {
it("should return properly values", () => {
assert.strictEqual(VueVersionEnum[2], 2);
assert.strictEqual(VueVersionEnum[3], 3);
});
});
21 changes: 7 additions & 14 deletions src/flows/guided-flow-generator.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { input } from "@inquirer/prompts";
import { checkDirectoriesTree } from "../utils/directory.js";
import { splitPathString } from "../utils/string.js";
import { generateComponent } from "../generators/component.js";
import { generateComponent } from "../generators/components.js";

/**
* Generate file from guided prompt
*
* @param {{
* type: string;
* name: string;
* framework: string;
* resourcePath: string;
* testPostfix: string;
* typescript: boolean;
* withTest: boolean;
* withStory: boolean;
* }} data Information the user provided in the guided prompt
* @param {{ exportDefault: boolean } | undefined} globalConfig
* @typedef {import("../actions/guided.js").Answers} Answers
*
* @param {Answers} data Information the user provided in the guided prompt
*/
export async function guidedFlowGenerator(data, globalConfig) {
export async function guidedFlowGenerator(data) {
await checkProvidedPathRecursive(data.resourcePath, (path) => {
generateComponent({ ...data, path });
});
Expand All @@ -31,7 +23,8 @@ export async function guidedFlowGenerator(data, globalConfig) {
* @returns {Promise<string>}
*/
async function checkProvidedPathRecursive(path, callback) {
const pathExists = checkDirectoriesTree(splitPathString(path));
const pathArray = splitPathString(path);
const pathExists = checkDirectoriesTree(pathArray);

if (pathExists) {
return callback(path);
Expand Down
99 changes: 0 additions & 99 deletions src/generators/component.js

This file was deleted.

Loading