Skip to content

Commit

Permalink
[cli] very rough working stylex cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Jta26 authored and Joel Austin committed Feb 10, 2024
1 parent aad6f0b commit d88b766
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 68 deletions.
102 changes: 40 additions & 62 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"@types/jest": "^29.5.11",
"babel-plugin-syntax-hermes-parser": "^0.18.2",
"benchmark": "^2.1.4",
"esbuild": "^0.19.12",
"cross-env": "^7.0.3",
"esbuild": "^0.19.12",
"eslint": "8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-ft-flow": "^3.0.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@stylexjs/cli",
"version": "0.5.1",
"description": "A cli to compile a folder with stylex",
"main": "./lib/index.js",
"main": "./src/index.js",
"repository": "https://www.github.com/facebook/stylex",
"license": "MIT",
"scripts": {
Expand All @@ -23,7 +23,7 @@
"@stylexjs/scripts": "0.5.1"
},
"bin": {
"stylex": "./lib/index.js"
"stylex": "./src/index.js"
},
"jest": {},
"files": [
Expand Down
18 changes: 18 additions & 0 deletions packages/cli/src/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

type Errors = $ReadOnly<{
dirNotFound: Error,
}>;

const errors: Errors = {
dirNotFound: new Error('Invalid Directory: Not Found'),
};

export default errors;
57 changes: 57 additions & 0 deletions packages/cli/src/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

// import type { Rule } from '@stylexjs/babel-plugin';

import fs from 'fs';
import path from 'path';
import errors from './errors';

// gets the directory for compiled styles (creates it if it doesn't exist)
export function makeCompiledDir(compiledDir: string): void {
if (!fs.existsSync(compiledDir)) {
console.log('made dir', compiledDir);
fs.mkdirSync(compiledDir);
}
}

export function getInputDirectoryFiles(inputDir: string): Array<string> {
if (!fs.existsSync(inputDir)) {
throw errors.dirNotFound;
} else {
const files = fs.readdirSync(inputDir, { recursive: true });
return files.filter((file) => {
const maybeDir = path.join(inputDir, file);
return !isDir(maybeDir);
});
}
}

// takes in the compiled rules and writes them to a file at the top of the compiled directory
export function writeCompiledCSS(filePath: string, compiledCSS: string): void {
fs.writeFileSync(filePath, compiledCSS);
}

export function writeCompiledJS(path: string, code: string): void {
makeDirExistRecursive(path);
fs.writeFileSync(path, code, {});
}

export function isDir(path: string): boolean {
return fs.lstatSync(path).isDirectory();
}

function makeDirExistRecursive(filePath: string): ?boolean {
const dirName = path.dirname(filePath);
if (fs.existsSync(dirName)) {
return true;
}
makeDirExistRecursive(dirName);
fs.mkdirSync(dirName);
}
Loading

0 comments on commit d88b766

Please sign in to comment.