css-codemod
Advanced tools
Comparing version 0.1.0-next.3 to 0.1.0-next.4
@@ -26,19 +26,43 @@ #!/usr/bin/env node | ||
// src/process-transform.ts | ||
// src/api.ts | ||
var import_postcss = __toESM(require("postcss")); | ||
var parse = (source) => { | ||
return import_postcss.default.parse(source); | ||
}; | ||
var api = { parse }; | ||
// src/files.ts | ||
var import_fs = __toESM(require("fs")); | ||
var import_glob = __toESM(require("glob")); | ||
var import_fs = __toESM(require("fs")); | ||
var getFileInfo = (file) => { | ||
try { | ||
const source = import_fs.default.readFileSync(file, "utf8").toString(); | ||
return { path: file, source }; | ||
} catch (err) { | ||
console.error(`An error occurred trying to read "${file}": ${err}`); | ||
process.exit(1); | ||
} | ||
}; | ||
var writeFile = (file, contents) => { | ||
try { | ||
import_fs.default.writeFileSync(file, contents); | ||
} catch (err) { | ||
console.error(`An error occurred trying to write "${file}": ${err}`); | ||
} | ||
}; | ||
var getAllFilesToTransform = (files) => import_glob.default.sync(files, { nodir: true }); | ||
// src/load-transform.ts | ||
// src/transform.ts | ||
var import_bundle_require = require("bundle-require"); | ||
var MINIMUM_EXAMPLE_TRANSFORM = ` | ||
var MINIMUM_TRANSFORM_EXAMPLE = ` | ||
import { Transform } from "css-codemod"; | ||
export const transform: Transform = (file, api) => {}; | ||
export const transform: Transform = (fileInfo, api) => {}; | ||
`; | ||
var validateTransform = (transform) => { | ||
if (typeof transform === "function") { | ||
if (typeof transform === "function" && transform.length <= 2) { | ||
return transform; | ||
} else { | ||
console.error(`Transform file must export a valid transform. For example: | ||
${MINIMUM_EXAMPLE_TRANSFORM}`); | ||
${MINIMUM_TRANSFORM_EXAMPLE}`); | ||
process.exit(1); | ||
@@ -58,19 +82,20 @@ } | ||
// src/api.ts | ||
var import_postcss = __toESM(require("postcss")); | ||
var parse = (source) => { | ||
return import_postcss.default.parse(source); | ||
}; | ||
var api = { parse }; | ||
// src/process-transform.ts | ||
var processTransform = async (options) => { | ||
// src/perform.ts | ||
var perform = async (options) => { | ||
const transform = await loadTransform(options.transform); | ||
const files = import_glob.default.sync(options.files); | ||
const files = getAllFilesToTransform(options.files); | ||
files.map((file) => { | ||
const source = import_fs.default.readFileSync(file, "utf8").toString(); | ||
const fileInfo = { path: file, source }; | ||
const result = transform(fileInfo, api); | ||
if (result !== null) { | ||
import_fs.default.writeFileSync(file, result); | ||
const fileInfo = getFileInfo(file); | ||
try { | ||
const result = transform(fileInfo, api); | ||
if (result !== null) { | ||
writeFile(file, result); | ||
} | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
console.error(`The following error occurred transforming "${file}": | ||
${err.message}`); | ||
} else { | ||
console.error(`An unexpected error occurred transforming "${file}": ${err}`); | ||
} | ||
} | ||
@@ -81,3 +106,3 @@ }); | ||
// src/cli.ts | ||
var PACKAGE_PATH = (0, import_path.join)(__dirname, "../package.json"); | ||
var PACKAGE_PATH = (0, import_path.join)(__dirname, "..", "package.json"); | ||
var PACKAGE_JSON = JSON.parse((0, import_fs2.readFileSync)(PACKAGE_PATH, "utf8")); | ||
@@ -92,3 +117,3 @@ var NAME = PACKAGE_JSON.name; | ||
const { transform } = flags; | ||
await processTransform({ files, transform }); | ||
await perform({ files, transform }); | ||
}); | ||
@@ -95,0 +120,0 @@ cli.help(); |
@@ -1,7 +0,7 @@ | ||
import { Plugin, Root } from 'postcss'; | ||
import { Root } from 'postcss'; | ||
declare type Processors = Omit<Plugin, 'postcssPlugin' | 'prepare'>; | ||
declare const traverse: (css: string, processors: Processors) => Promise<string | null>; | ||
interface TransformAPI { | ||
/** | ||
* Parse a raw CSS string into an abstract syntax tree and return a PostCSS `Root` node. | ||
*/ | ||
parse(source: string): Root; | ||
@@ -20,2 +20,3 @@ } | ||
} | ||
declare type Transform = ( | ||
@@ -25,4 +26,8 @@ /** | ||
*/ | ||
fileInfo: TransformFileInfo, api: TransformAPI) => null | string; | ||
fileInfo: TransformFileInfo, | ||
/** | ||
* Helpers injected by css-codemod. | ||
*/ | ||
api: TransformAPI) => null | string; | ||
export { Processors, Transform, traverse }; | ||
export { Transform }; |
@@ -1,26 +0,6 @@ | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __reExport = (target, module2, copyDefault, desc) => { | ||
@@ -34,5 +14,2 @@ if (module2 && typeof module2 === "object" || typeof module2 === "function") { | ||
}; | ||
var __toESM = (module2, isNodeMode) => { | ||
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); | ||
}; | ||
var __toCommonJS = /* @__PURE__ */ ((cache) => { | ||
@@ -46,34 +23,5 @@ return (module2, temp) => { | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
traverse: () => traverse | ||
}); | ||
// src/traverse/index.ts | ||
var import_postcss = __toESM(require("postcss")); | ||
var createTransformPlugin = (processors) => __spreadValues({ | ||
postcssPlugin: "css-codemod-plugin" | ||
}, processors); | ||
var traverse = async (css, processors) => { | ||
try { | ||
const plugins = [createTransformPlugin(processors)]; | ||
const processor = (0, import_postcss.default)(plugins); | ||
const result = await processor.process(css, { | ||
from: void 0 | ||
}); | ||
return result.css; | ||
} catch (err) { | ||
console.error(`Unexpected error processing CSS. | ||
CSS string: | ||
${css} | ||
Error: | ||
${err}`); | ||
} | ||
return null; | ||
}; | ||
// src/transform.ts | ||
var import_bundle_require = require("bundle-require"); | ||
module.exports = __toCommonJS(src_exports); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
traverse | ||
}); |
{ | ||
"version": "0.1.0-next.3", | ||
"version": "0.1.0-next.4", | ||
"license": "MIT", | ||
@@ -22,2 +22,3 @@ "name": "css-codemod", | ||
"test": "tsdx test", | ||
"test:watch": "tsdx test --watch", | ||
"lint": "tsdx lint", | ||
@@ -39,6 +40,12 @@ "prepare": "yarn build", | ||
}, | ||
"resolutions": { | ||
"**/tsdx/jest": "27.4.7", | ||
"**/tsdx/ts-jest": "27.1.3" | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "^9.0.13", | ||
"@types/glob": "^7.2.0", | ||
"@types/node": "^17.0.13", | ||
"dripip": "^0.10.0", | ||
"execa": "^5.1.1", | ||
"husky": "^7.0.4", | ||
@@ -55,2 +62,3 @@ "rimraf": "^3.0.2", | ||
"esbuild": "^0.14.14", | ||
"fs-extra": "^10.0.0", | ||
"glob": "^7.2.0", | ||
@@ -57,0 +65,0 @@ "postcss": "^8.4.5" |
@@ -5,5 +5,5 @@ # :snake: css-codemod | ||
## Usage | ||
## Install | ||
There are two ways to use css-codemod. | ||
There are a few ways to use css-codemod. | ||
@@ -19,7 +19,2 @@ First, using [npx](https://www.npmjs.com/package/npx) to execute the transform without need to explicitly install `css-codemod`. | ||
```bash | ||
# Install and execute css-codemod with npm | ||
npm i -D css-codemod | ||
./node_modules/.bin/css-codemod "./src/**/*.css" -t ./transform.ts | ||
# Or, install and execute css-codemod with yarn | ||
yarn add -D css-codemod | ||
@@ -29,23 +24,13 @@ yarn css-codemod "./src/**/*.css" -t ./transform.ts | ||
## Transform | ||
Third, install `css-codemod` globally. | ||
The transform file defines the transformation to make to each file. The transform can be written in either JavaScript or TypeScript. | ||
``` | ||
yarn add -g css-codemod | ||
css-codemod "./src/**/*.css" -t ./transform.ts | ||
``` | ||
It needs to provide a named `transform` export that is a function. This transform function will be invoked for each file that matches the files to process. | ||
## Usage (CLI) | ||
```ts | ||
// transform.ts | ||
The CLI provides the following options: | ||
// Import the `Transform` type to provide type-safety when | ||
// using and creating a transform function. | ||
import { Transform } from 'css-codemod'; | ||
// Define a named `transform` export function. | ||
export const transform: Transform = (fileInfo, api) => { | ||
// Implement the transform. | ||
}; | ||
``` | ||
## CLI | ||
```bash | ||
@@ -73,2 +58,24 @@ Usage: | ||
This will pass the source of all files through the transform function specified with `-t` or `--transform` (defaults to `./transform.ts`). See the following sections for more details on the transform function and API. | ||
## Transform | ||
The transform function defines the transformation to make to each file. The transform can be written in either JavaScript or TypeScript, but TypeScript is recommended. | ||
The transform function needs to be a named `transform` export from the transform file. | ||
```ts | ||
// transform.ts | ||
// Import the `Transform` type to provide type-safety when | ||
// using and creating a transform function. | ||
import { Transform } from 'css-codemod'; | ||
// Define a named `transform` export function. | ||
export const transform: Transform = (fileInfo, api) => { | ||
// Implement the transform. | ||
// See below for more details on the API. | ||
}; | ||
``` | ||
## API | ||
@@ -75,0 +82,0 @@ |
import postcss, { Root } from 'postcss'; | ||
export interface TransformAPI { | ||
/** | ||
* Parse a raw CSS string into an abstract syntax tree and return a PostCSS `Root` node. | ||
*/ | ||
parse(source: string): Root; | ||
@@ -5,0 +8,0 @@ } |
@@ -6,5 +6,5 @@ #!/usr/bin/env node | ||
import { join } from 'path'; | ||
import { processTransform } from './process-transform'; | ||
import { perform } from './perform'; | ||
const PACKAGE_PATH = join(__dirname, '../package.json'); | ||
const PACKAGE_PATH = join(__dirname, '..', 'package.json'); | ||
const PACKAGE_JSON = JSON.parse(readFileSync(PACKAGE_PATH, 'utf8')); | ||
@@ -32,3 +32,3 @@ const NAME = PACKAGE_JSON.name; | ||
await processTransform({ files, transform }); | ||
await perform({ files, transform }); | ||
}); | ||
@@ -35,0 +35,0 @@ |
@@ -1,2 +0,1 @@ | ||
export { traverse, Processors } from './traverse'; | ||
export { Transform } from './transform'; |
@@ -0,14 +1,10 @@ | ||
import { bundleRequire } from 'bundle-require'; | ||
import { TransformAPI } from './api'; | ||
import { TransformFileInfo } from './files'; | ||
export interface TransformFileInfo { | ||
/** | ||
* The file path for the current file being transformed. | ||
*/ | ||
path: string; | ||
const MINIMUM_TRANSFORM_EXAMPLE = ` | ||
import { Transform } from "css-codemod"; | ||
/** | ||
* The file contents for the current file being transformed. | ||
*/ | ||
source: string; | ||
} | ||
export const transform: Transform = (fileInfo, api) => {}; | ||
`; | ||
@@ -20,3 +16,37 @@ export type Transform = ( | ||
fileInfo: TransformFileInfo, | ||
/** | ||
* Helpers injected by css-codemod. | ||
*/ | ||
api: TransformAPI | ||
) => null | string; | ||
/** | ||
* Validate the general structure of the transform to catch simple errors. | ||
*/ | ||
export const validateTransform = (transform: unknown): Transform => { | ||
if (typeof transform === 'function' && transform.length <= 2) { | ||
return transform as Transform; | ||
} else { | ||
console.error( | ||
`Transform file must export a valid transform. For example:\n${MINIMUM_TRANSFORM_EXAMPLE}` | ||
); | ||
process.exit(1); | ||
} | ||
}; | ||
/** | ||
* Load and validate the transform file given the filepath. | ||
*/ | ||
export const loadTransform = async (filepath: string): Promise<Transform> => { | ||
try { | ||
const { mod } = await bundleRequire({ filepath }); | ||
const transform = validateTransform(mod.transform || mod.default); | ||
return transform; | ||
} catch (err) { | ||
console.error( | ||
`An error occurred loading the transform file. Verify "${filepath}" exists.` | ||
); | ||
process.exit(1); | ||
} | ||
}; |
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
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
149
18820
6
11
13
+ Addedfs-extra@^10.0.0
+ Addedfs-extra@10.1.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addeduniversalify@2.0.1(transitive)