@futura-dev/cosmofactory
Advanced tools
Comparing version 0.0.0 to 0.1.0
#! /usr/bin/env node | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const commander_1 = require("commander"); | ||
const init_1 = require("./commands/init"); | ||
const process = tslib_1.__importStar(require("process")); | ||
const build_1 = require("./commands/build"); | ||
var tslib_1 = require("tslib"); | ||
var commander_1 = require("commander"); | ||
var init_1 = require("./commands/init"); | ||
var process = tslib_1.__importStar(require("process")); | ||
var build_1 = require("./commands/build"); | ||
// program definition | ||
commander_1.program.name("@futura-dev/cosmofactory").description("Cosmofactory ๐ฆ"); | ||
commander_1.program.name('@futura-dev/cosmofactory').description('Cosmofactory ๐ฆ'); | ||
// 'init' command definition | ||
commander_1.program.command("init").action(async () => (0, init_1.init)()); | ||
commander_1.program.command('init').action(async () => (0, init_1.init)()); | ||
// 'build' command definition | ||
commander_1.program.command("build").action(async () => (0, build_1.build)()); | ||
commander_1.program.command('build').action(async () => (0, build_1.build)()); | ||
// parse program | ||
commander_1.program.parse(process.argv); |
@@ -0,1 +1,4 @@ | ||
/** | ||
* | ||
*/ | ||
export declare const build: () => Promise<void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.build = void 0; | ||
const tslib_1 = require("tslib"); | ||
const fs = tslib_1.__importStar(require("fs")); | ||
const path = tslib_1.__importStar(require("path")); | ||
const tsconfig_base_json_1 = tslib_1.__importDefault(require("../config/tsconfig.base.json")); | ||
const typescript_1 = tslib_1.__importDefault(require("typescript")); | ||
const child_process_1 = require("child_process"); | ||
const cosmofactory_config_1 = require("../utils/validation/cosmofactory-config"); | ||
const build = async () => { | ||
/** | ||
* Returns a list of all files in the subdirectories of the given directory. | ||
*/ | ||
const getFilesRecursive = (dir, baseDir = "", extensions = ["ts", "tsx"], where = "src") => { | ||
const files = []; | ||
fs.readdirSync(dir).forEach((file) => { | ||
const filePath = path.join(dir, file); | ||
const stat = fs.statSync(filePath); | ||
const isExtensionMatched = extensions.reduce((acc, ext) => { | ||
return acc || file.endsWith(ext); | ||
}, false); | ||
if (stat.isDirectory()) { | ||
const newBaseDir = path.join(baseDir, file); | ||
files.push(...getFilesRecursive(filePath, newBaseDir, extensions, where)); | ||
} | ||
else if (stat.isFile() && isExtensionMatched) { | ||
const relativeDir = baseDir ? path.join(baseDir, file) : file; | ||
files.push(`${dirname}/../${where}/${relativeDir}`); | ||
} | ||
}); | ||
return files; | ||
}; | ||
const standardizePath = (path) => { | ||
if (path === undefined) | ||
path = ""; | ||
let str = [".", ""].includes(path) ? "./" : path; | ||
if (str.charAt(str.length - 1) !== "/") | ||
str += "/"; | ||
return str; | ||
}; | ||
/** | ||
* Replaces absolute paths with relative paths in the generated JavaScript files. | ||
*/ | ||
const replaceAbsolutePaths = () => { | ||
const jsFiles = getFilesRecursive(distDir, "", ["js"], "dist").filter((file) => file.endsWith(".js")); | ||
jsFiles.forEach((jsFile) => { | ||
const filePath = path.join(jsFile); | ||
const fileContent = fs.readFileSync(filePath, "utf8"); | ||
const computedBaseUrl = standardizePath(compilerOptions.baseUrl); | ||
const relative = new Array(...(jsFile.replace(distDir, "").match(/(\/)/g)?.slice(0, -1) ?? [])).reduce((acc) => { | ||
return acc + "../"; | ||
}, ""); | ||
const updatedContent = Object.entries(compilerOptions.paths ?? {}).reduce((content, [key, values]) => { | ||
// TODO; implement fallback values | ||
const replacementPath = relative + | ||
(computedBaseUrl + values[0]) | ||
.replace(/(.*)src\/(.*)/, "$2") | ||
.replace("*", ""); | ||
const pattern = new RegExp(`${key === "@/*" ? "@/" : key.replace("$/*", "")}/?(.*?)`, "g"); | ||
return content.replace(pattern, `${replacementPath}$1`); | ||
}, fileContent); | ||
fs.writeFileSync(filePath, updatedContent, "utf8"); | ||
}); | ||
}; | ||
// TODO: Check this function | ||
const getNormalizedPath = (path = "") => path.replace(/\/\.$/g, "").replace(/\/$/g, ""); | ||
const isFile = (path = "") => { | ||
const normalizedPath = getNormalizedPath(path); | ||
return (normalizedPath.includes(".") && | ||
normalizedPath.charAt(normalizedPath.length - 1) !== "."); | ||
}; | ||
const pathToWriteOptions = (path = "") => { | ||
const normalizedPath = getNormalizedPath(path); | ||
if (isFile(path)) { | ||
const segmentedPath = normalizedPath.split("/"); | ||
return { | ||
directory: segmentedPath.slice(0, segmentedPath.length - 1).join("/"), | ||
fileName: segmentedPath.slice(-1)[0], | ||
}; | ||
var tslib_1 = require("tslib"); | ||
var fs = tslib_1.__importStar(require("fs")); | ||
var path = tslib_1.__importStar(require("path")); | ||
var typescript_1 = tslib_1.__importDefault(require("typescript")); | ||
var child_process_1 = require("child_process"); | ||
var cosmofactory_config_1 = require("../utils/validation/cosmofactory-config"); | ||
/** | ||
* | ||
* @param path | ||
*/ | ||
const getNormalizedPath = (path = "") => path.replace(/\/\.$/g, "").replace(/\/$/g, ""); | ||
/** | ||
* | ||
* @param path | ||
*/ | ||
const isFile = (path = "") => { | ||
const normalizedPath = getNormalizedPath(path); | ||
return (normalizedPath.includes(".") && | ||
normalizedPath.charAt(normalizedPath.length - 1) !== "."); | ||
}; | ||
/** | ||
* Function that parse a path to create a standard string representing the path | ||
* actions: | ||
* - './' for empty path | ||
* - '/' at the end of the path | ||
* @param path - path to standardize | ||
*/ | ||
const standardizePath = (path) => { | ||
if (path === undefined) | ||
path = ""; | ||
let str = [".", ""].includes(path) ? "./" : path; | ||
if (str.charAt(str.length - 1) !== "/") | ||
str += "/"; | ||
return str; | ||
}; | ||
const initWithDotSlash = (path) => { | ||
return /^\.\//g.test(path) ? path : `./${path}`; | ||
}; | ||
/** | ||
* Function that recursively find the path of file with the given extension | ||
* @param where - the folder to use as root of search | ||
* @param extensions - an array of extensions without . character | ||
*/ | ||
const getFilesRecursive = (where, extensions = ["ts", "tsx"]) => { | ||
// pool to contain files | ||
const files = []; | ||
fs.readdirSync(where).forEach(file => { | ||
const filePath = path.join(where, file); | ||
const stat = fs.statSync(filePath); | ||
const isExtensionMatched = extensions.reduce((acc, ext) => { | ||
return acc || file.endsWith(ext); | ||
}, false); | ||
if (stat.isDirectory()) { | ||
files.push(...getFilesRecursive(filePath, extensions)); | ||
} | ||
else { | ||
return { | ||
directory: normalizedPath, | ||
fileName: undefined, | ||
}; | ||
else if (stat.isFile() && isExtensionMatched) { | ||
files.push(`${filePath}`); | ||
} | ||
}; | ||
}); | ||
return files; | ||
}; | ||
const pathToWriteOptions = (path = "") => { | ||
const normalizedPath = getNormalizedPath(path); | ||
if (isFile(path)) { | ||
const segmentedPath = normalizedPath.split("/"); | ||
return { | ||
directory: segmentedPath.slice(0, segmentedPath.length - 1).join("/"), | ||
fileName: segmentedPath.slice(-1)[0] | ||
}; | ||
} | ||
else { | ||
return { | ||
directory: normalizedPath, | ||
fileName: undefined | ||
}; | ||
} | ||
}; | ||
/** | ||
* | ||
*/ | ||
const build = async () => { | ||
// STEP 0 | ||
@@ -95,27 +91,10 @@ // Load the configuration | ||
} | ||
// validate the configuration | ||
const configuration = cosmofactory_config_1.cosmofactory_config_schema.parse(JSON.parse(fs.readFileSync("./.cosmofactory.json", { encoding: "utf8" }))); | ||
// Set the input and output directory paths | ||
const dirname = __dirname; | ||
const srcDir = dirname + "/../src"; | ||
const distDir = dirname + "/../dist"; | ||
// TypeScript compilation options | ||
const compilerOptions = { | ||
target: typescript_1.default.ScriptTarget.ES5, | ||
module: typescript_1.default.ModuleKind.ESNext, | ||
outDir: distDir, | ||
declaration: true, | ||
declarationDir: distDir, | ||
baseUrl: tsconfig_base_json_1.default.compilerOptions.baseUrl, | ||
paths: tsconfig_base_json_1.default.compilerOptions.paths, | ||
/* Bundler mode */ | ||
moduleResolution: typescript_1.default.ModuleResolutionKind.NodeNext, | ||
resolveJsonModule: true, | ||
isolatedModules: true, | ||
jsx: typescript_1.default.JsxEmit.ReactJSX, | ||
/* Linting */ | ||
strict: true, | ||
noUnusedLocals: true, | ||
noUnusedParameters: true, | ||
noFallthroughCasesInSwitch: true, | ||
}; | ||
// load the tsconfig | ||
const tsConfig = JSON.parse(fs.readFileSync("./tsconfig.json", { encoding: "utf8" })); | ||
// Set the input and output directory pathss | ||
const srcDir = "./src"; | ||
const distDir = `${initWithDotSlash(tsConfig.compilerOptions.outDir)}`; | ||
const compilerOptions = tsConfig.compilerOptions; | ||
// Create the output directory if it doesn't exist | ||
@@ -129,3 +108,3 @@ if (!fs.existsSync(distDir)) { | ||
const program = typescript_1.default.createProgram(files, compilerOptions); | ||
// Run the compilation | ||
// Run the transpiling | ||
const emitResult = program.emit(); | ||
@@ -136,4 +115,4 @@ // Handle compilation errors | ||
.concat(emitResult.diagnostics); | ||
allDiagnostics.forEach((diagnostic) => { | ||
if (diagnostic.file) { | ||
allDiagnostics.forEach(diagnostic => { | ||
if (diagnostic.file && diagnostic.start) { | ||
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); | ||
@@ -156,3 +135,3 @@ const message = typescript_1.default.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); | ||
if (!fs.existsSync(source) || fs.lstatSync(source).isDirectory()) { | ||
throw new Error("Source does not exist or is a directory"); | ||
throw new Error(`๐งจโ ๏ธ๐ฃ Source ${source} does not exist or is a directory`); | ||
} | ||
@@ -168,3 +147,22 @@ const destination = `${distDir}/${configuration.files[source]}`; | ||
// Replace absolute paths to relative paths in the generated JavaScript files | ||
replaceAbsolutePaths(); | ||
// TODO: check that | ||
const jsFiles = getFilesRecursive(distDir, ["js"]).filter(file => file.endsWith(".js")); | ||
jsFiles.forEach(jsFile => { | ||
const filePath = path.join(jsFile); | ||
const fileContent = fs.readFileSync(filePath, "utf8"); | ||
const computedBaseUrl = standardizePath(compilerOptions.baseUrl); | ||
const relative = new Array(...(jsFile.replace(distDir, "").match(/(\/)/g)?.slice(0, -1) ?? [])).reduce(acc => { | ||
return acc + "../"; | ||
}, ""); | ||
const updatedContent = Object.entries(compilerOptions.paths ?? {}).reduce((content, [key, values]) => { | ||
// TODO; implement fallback values | ||
const replacementPath = relative + | ||
(computedBaseUrl + values[0]) | ||
.replace(/(.*)src\/(.*)/, "$2") | ||
.replace("*", ""); | ||
const pattern = new RegExp(`${key === "../*" ? "../" : key.replace("$/*", "")}/?(.*?)`, "g"); | ||
return content.replace(pattern, `${replacementPath}$1`); | ||
}, fileContent); | ||
fs.writeFileSync(filePath, updatedContent, "utf8"); | ||
}); | ||
// manage tailwindss style | ||
@@ -178,3 +176,3 @@ if (configuration.tailwind) { | ||
"build", | ||
"--minify", | ||
"--minify" | ||
]); | ||
@@ -181,0 +179,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.init = void 0; | ||
const tslib_1 = require("tslib"); | ||
const fs = tslib_1.__importStar(require("fs")); | ||
const functions_1 = require("../utils/functions"); | ||
var tslib_1 = require("tslib"); | ||
var fs = tslib_1.__importStar(require("fs")); | ||
var functions_1 = require("../utils/functions"); | ||
const init = async () => { | ||
@@ -15,3 +15,3 @@ let does_override = true; | ||
active: "yes", | ||
inactive: "no", | ||
inactive: "no" | ||
})).does_override; | ||
@@ -25,5 +25,5 @@ } | ||
"plugin-tailwind.ts": "./", | ||
"tailwind.config.ts": "./", | ||
"tailwind.config.ts": "./" | ||
}, | ||
tailwind: true, | ||
tailwind: true | ||
}, null, 2)); | ||
@@ -30,0 +30,0 @@ console.log(".cosmofactory.json file was successfully created"); |
/// <reference types="node" /> | ||
import { ParamsOf } from "./types"; | ||
import prompts from "prompts"; | ||
import { ParamsOf } from './types'; | ||
import prompts from 'prompts'; | ||
/** | ||
@@ -10,7 +10,2 @@ * controlledSpawn | ||
/** | ||
* isArray | ||
* @param source | ||
*/ | ||
export declare const isArray: (source: any) => source is any[]; | ||
/** | ||
* ask | ||
@@ -17,0 +12,0 @@ * @param questions |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ask = exports.isArray = exports.controlledSpawn = void 0; | ||
const tslib_1 = require("tslib"); | ||
const child_process_1 = require("child_process"); | ||
const prompts_1 = tslib_1.__importDefault(require("prompts")); | ||
exports.ask = exports.controlledSpawn = void 0; | ||
var tslib_1 = require("tslib"); | ||
var child_process_1 = require("child_process"); | ||
var prompts_1 = tslib_1.__importDefault(require("prompts")); | ||
/** | ||
@@ -12,7 +12,7 @@ * controlledSpawn | ||
const controlledSpawn = (...params) => { | ||
params[2] && !params[2].encoding && (params[2].encoding = "utf8"); | ||
params[2] && !params[2].encoding && (params[2].encoding = 'utf8'); | ||
const output = (0, child_process_1.spawnSync)(...params); | ||
if (output.status !== 0) { | ||
console.log(output.error); | ||
console.log(output.stdout?.toString() ?? ""); | ||
console.log(output.stdout?.toString() ?? ''); | ||
console.log(output); | ||
@@ -25,10 +25,2 @@ throw new Error(output.stderr.toString()); | ||
/** | ||
* isArray | ||
* @param source | ||
*/ | ||
const isArray = (source) => { | ||
return Array.isArray(source); | ||
}; | ||
exports.isArray = isArray; | ||
/** | ||
* ask | ||
@@ -41,7 +33,7 @@ * @param questions | ||
...options, | ||
onCancel: (...args) => { | ||
onCancel: () => { | ||
process.exit(0); | ||
}, | ||
} | ||
}); | ||
}; | ||
exports.ask = ask; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cosmofactory_config_schema = void 0; | ||
const zod_1 = require("zod"); | ||
var zod_1 = require("zod"); | ||
exports.cosmofactory_config_schema = zod_1.z.object({ | ||
files: zod_1.z.any(), | ||
tailwind: zod_1.z.boolean(), | ||
tailwind: zod_1.z.boolean() | ||
}); |
{ | ||
"name": "@futura-dev/cosmofactory", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"private": false, | ||
@@ -25,3 +25,6 @@ "repository": { | ||
"scripts": { | ||
"build": "rm -rf dist && npx tsc" | ||
"build": "rm -rf dist && npx tsx src/cli.ts build", | ||
"lint": "eslint ./src --ext .ts --config .eslintrc", | ||
"lint:fix": "npm run lint -- --fix", | ||
"lint:beautify": "npm run lint:fix && prettier ./src --write" | ||
}, | ||
@@ -41,2 +44,3 @@ "dependencies": { | ||
"@futura-dev/cosmokeeper": "^0.1.0", | ||
"@futura-dev/eslint-config-typescript": "^0.1.2", | ||
"@types/commander": "^2.12.2", | ||
@@ -43,0 +47,0 @@ "@types/mute-stream": "^0.0.1", |
@@ -1,2 +0,2 @@ | ||
# Cosmofactory | ||
# ๐ฆ Cosmofactory | ||
@@ -3,0 +3,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18662
10
301
3