Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@codemod.com/workflow

Package Overview
Dependencies
Maintainers
2
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@codemod.com/workflow - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

dist/astGrep/getImports.d.ts

53

dist/astGrep/astGrep.d.ts

@@ -9,3 +9,55 @@ import { type NapiConfig } from "@ast-grep/napi";

};
declare const astGrepAPIHelpers: {
astGrep: typeof astGrep;
};
type AstGrepPattern = string | {
selector: string;
context: string;
};
type AstGrepStopBy = "end" | "neighbour" | AstGrepInsideRule;
type AstGrepInsideRule = {
pattern: AstGrepPattern;
stopBy?: AstGrepStopBy;
field?: string;
};
type AstGrepHasRule = {
kind: string;
stopBy?: AstGrepStopBy;
field?: string;
};
type AstGrepSiblingRule = {
kind: string;
stopBy?: AstGrepStopBy;
};
type AstGrepAtomicRule = {
pattern: AstGrepPattern;
kind?: string;
regex?: string;
inside?: AstGrepInsideRule;
has?: AstGrepHasRule;
precedes?: AstGrepSiblingRule;
follows?: AstGrepSiblingRule;
};
type AstGrepAllRule = {
all: AstGrepRules[];
};
type AstGrepAnyRule = {
any: AstGrepRules[];
};
type AstGrepNotRule = {
not: AstGrepRules;
};
type AstGrepMatchRule = {
match: string;
};
type AstGrepRules = AstGrepAtomicRule | AstGrepAllRule | AstGrepAnyRule | AstGrepNotRule | AstGrepMatchRule;
type AstGrepAPI = {
id: string;
language: "Bash" | "C" | "Cpp" | "CSharp" | "Css" | "Dart" | "Elixir" | "Go" | "Html" | "Java" | "JavaScript" | "Json" | "Kotlin" | "Lua" | "Php" | "Python" | "Ruby" | "Rust" | "Scala" | "Swift" | "TypeScript" | "Tsx";
rule: AstGrepRules;
utils?: any;
fix?: any;
};
type AstGrepHelpers = typeof astGrepHelpers;
type AstGrepAPIHelpers = typeof astGrepAPIHelpers;
/**

@@ -31,2 +83,3 @@ *

export declare function astGrep(query: string | readonly string[] | NapiConfig): PLazy<AstGrepHelpers> & AstGrepHelpers;
export declare function astGrep(apiQuery: AstGrepAPI): PLazy<AstGrepAPIHelpers> & AstGrepAPIHelpers;
/**

@@ -33,0 +86,0 @@ *

64

dist/astGrep/astGrep.js

@@ -34,6 +34,13 @@ "use strict";

};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.astGrep = void 0;
const childProcess = __importStar(require("node:child_process"));
const fs = __importStar(require("node:fs/promises"));
const path = __importStar(require("node:path"));
const util = __importStar(require("node:util"));
const napi_1 = require("@ast-grep/napi");
const magic_string_1 = __importDefault(require("magic-string"));
const PLazy_js_1 = require("../PLazy.js");

@@ -48,2 +55,18 @@ const contexts_js_1 = require("../contexts.js");

};
const astGrepAPIHelpers = {
astGrep,
};
const execFile = util.promisify(childProcess.execFile);
const runExternalAstGrepRule = (rule, filepath) => __awaiter(void 0, void 0, void 0, function* () {
const packageJsonFilepath = require.resolve("@ast-grep/cli/package.json");
const { bin: binaries } = JSON.parse(yield fs.readFile(packageJsonFilepath, "utf-8"));
const binaryPath = path.join(path.dirname(require.resolve("@ast-grep/cli/package.json")), binaries.sg);
yield execFile(binaryPath, [
"scan",
"--update-all",
"--inline-rules",
JSON.stringify(rule),
filepath,
]);
});
function astGrep(query, callback) {

@@ -53,20 +76,36 @@ const innerParentContext = (0, contexts_js_1.getParentContext)();

if (typeof query === "object") {
grep = Array.isArray(query) ? query.join("") : query;
grep = Array.isArray(query)
? query.join("")
: query;
}
const returnHelpers = typeof grep === "object" && "id" in grep
? astGrepAPIHelpers
: astGrepHelpers;
const context = (cb) => __awaiter(this, void 0, void 0, function* () {
yield innerParentContext(() => __awaiter(this, void 0, void 0, function* () {
const { file } = (0, contexts_js_1.getFileContext)();
const contents = yield fs.readFile(file, { encoding: "utf-8" });
const nodes = napi_1.tsx.parse(contents).root().findAll(grep);
const astContext = { contents };
for (const node of nodes) {
if (cb) {
astContext.node = node;
yield contexts_js_1.astGrepNodeContext.run(astContext, cb, callback ? astGrepHelpers : (0, helpers_js_1.wrapHelpers)(astGrepHelpers, context));
if (typeof grep === "object" && "id" in grep) {
yield runExternalAstGrepRule(grep, file);
}
else {
const contents = yield fs.readFile(file, { encoding: "utf-8" });
const nodes = napi_1.tsx.parse(contents).root().findAll(grep).reverse();
const astContext = {
contents: new magic_string_1.default(contents),
};
for (const node of nodes) {
if (cb) {
astContext.node = node;
yield contexts_js_1.astGrepNodeContext.run(astContext, cb, callback ? returnHelpers : (0, helpers_js_1.wrapHelpers)(returnHelpers, context));
}
}
if (astContext.contents.hasChanged()) {
yield fs.writeFile(file, astContext.contents.toString());
console.log(`${helpers_js_1.clc.blueBright("FILE")} ${file}`);
}
}
}));
return (0, helpers_js_1.wrapHelpers)(astGrepHelpers, context);
return (0, helpers_js_1.wrapHelpers)(returnHelpers, context);
});
const helpers = (0, helpers_js_1.wrapHelpers)(astGrepHelpers, context);
const helpers = (0, helpers_js_1.wrapHelpers)(returnHelpers, context);
const promise = new PLazy_js_1.PLazy((resolve, reject) => {

@@ -77,3 +116,4 @@ if (callback) {

voidOrPromise
.then(() => resolve((0, helpers_js_1.wrapHelpers)(astGrepHelpers, context)))
// @ts-ignore
.then(() => resolve((0, helpers_js_1.wrapHelpers)(returnHelpers, context)))
.catch(reject);

@@ -83,2 +123,3 @@ }

else {
// @ts-ignore
context().then(resolve).catch(reject);

@@ -91,2 +132,3 @@ }

});
// @ts-ignore
return promise;

@@ -93,0 +135,0 @@ }

declare const helpers: {
getNode: () => import("@ast-grep/napi").SgNode;
getMatch: (m: string) => import("@ast-grep/napi").SgNode | null;
getMultipleMatches: (m: string) => import("@ast-grep/napi").SgNode[];
};

@@ -4,0 +6,0 @@ type Helpers = typeof helpers;

@@ -16,3 +16,7 @@ "use strict";

const helpers_js_1 = require("../helpers.js");
const helpers = { getNode: () => (0, contexts_js_1.getAstGrepNodeContext)().node };
const helpers = {
getNode: () => (0, contexts_js_1.getAstGrepNodeContext)().node,
getMatch: (m) => (0, contexts_js_1.getAstGrepNodeContext)().node.getMatch(m),
getMultipleMatches: (m) => (0, contexts_js_1.getAstGrepNodeContext)().node.getMultipleMatches(m),
};
function map(callback) {

@@ -19,0 +23,0 @@ const innerParentContext = (0, contexts_js_1.getParentContext)();

import { PLazy } from "../PLazy.js";
import { map } from "./map.js";
declare const callbackHelpers: {
getNode: () => import("@ast-grep/napi").SgNode;
getMatch: (m: string) => import("@ast-grep/napi").SgNode | null;
getMultipleMatches: (m: string) => import("@ast-grep/napi").SgNode[];
};
declare const helpers: {

@@ -7,3 +12,4 @@ map: typeof map;

type Helpers = typeof helpers;
export declare function replace(callback: (helpers: typeof callbackHelpers) => Promise<string | undefined> | string | undefined): PLazy<Helpers> & Helpers;
export declare function replace(rawReplacement: string | Readonly<string[]>): PLazy<Helpers> & Helpers;
export {};

67

dist/astGrep/replace.js
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

@@ -36,3 +13,2 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }

exports.replace = void 0;
const fs = __importStar(require("node:fs/promises"));
const PLazy_js_1 = require("../PLazy.js");

@@ -42,29 +18,38 @@ const contexts_js_1 = require("../contexts.js");

const map_js_1 = require("./map.js");
const callbackHelpers = {
getNode: () => (0, contexts_js_1.getAstGrepNodeContext)().node,
getMatch: (m) => (0, contexts_js_1.getAstGrepNodeContext)().node.getMatch(m),
getMultipleMatches: (m) => (0, contexts_js_1.getAstGrepNodeContext)().node.getMultipleMatches(m),
};
const helpers = { map: map_js_1.map };
function replace(rawReplacement) {
function replace(replacementOrCallback) {
const innerParentContext = (0, contexts_js_1.getParentContext)();
const replacement = typeof rawReplacement === "string"
? rawReplacement
: rawReplacement.join("");
let replacement;
if (typeof replacementOrCallback === "string") {
replacement = replacementOrCallback;
}
else if (Array.isArray(replacementOrCallback)) {
replacement = replacementOrCallback.join("");
}
const callback = typeof replacementOrCallback === "function"
? replacementOrCallback
: undefined;
const context = (cb) => __awaiter(this, void 0, void 0, function* () {
yield innerParentContext(() => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const astGrepNodeContext = (0, contexts_js_1.getAstGrepNodeContext)();
if (astGrepNodeContext.node) {
const { node } = (0, contexts_js_1.getAstGrepNodeContext)();
if (callback) {
replacement = yield callback((0, helpers_js_1.wrapHelpers)(callbackHelpers, context));
}
if (replacement) {
const text = replacement.replace(/(\$\$)?\$([A-Z]+)/gm,
// @ts-ignore
(match, isMultiMatch, varName) => {
var _a, _b, _c;
var _a;
if (isMultiMatch) {
return (_a = astGrepNodeContext.node) === null || _a === void 0 ? void 0 : _a.getMultipleMatches(varName).map((n) => n.text()).join(" ");
return node === null || node === void 0 ? void 0 : node.getMultipleMatches(varName).map((n) => n.text()).join(" ");
}
return ((_c = (_b = astGrepNodeContext.node) === null || _b === void 0 ? void 0 : _b.getMatch(varName)) === null || _c === void 0 ? void 0 : _c.text()) || "";
return ((_a = node.getMatch(varName)) === null || _a === void 0 ? void 0 : _a.text()) || "";
});
const transformed = astGrepNodeContext.contents.substring(0, (_a = astGrepNodeContext.node) === null || _a === void 0 ? void 0 : _a.range().start.index) +
text +
astGrepNodeContext.contents.substring(((_b = astGrepNodeContext.node) === null || _b === void 0 ? void 0 : _b.range().end.index) || 0);
const { file } = (0, contexts_js_1.getFileContext)();
astGrepNodeContext.contents = transformed;
yield fs.writeFile(file, transformed);
console.log(`${helpers_js_1.clc.blueBright("FILE")} ${file}`);
const range = node.range();
(0, contexts_js_1.getAstGrepNodeContext)().contents.update(range.start.index, range.end.index, text);
}

@@ -71,0 +56,0 @@ }));

/// <reference types="node" />
import { AsyncLocalStorage } from "node:async_hooks";
import type { SgNode } from "@ast-grep/napi";
import type MagicString from "magic-string";
export declare const registerContext: <T>(name: string, ctx: AsyncLocalStorage<T>) => AsyncLocalStorage<T>;
export type AstGrepNodeContext = {
node: SgNode;
contents: string;
contents: MagicString;
};

@@ -23,2 +24,6 @@ export declare const parentContext: AsyncLocalStorage<(...args: any[]) => any>;

file: string;
importsUpdates: {
type: "add" | "remove";
import: string;
}[];
}>;

@@ -39,2 +44,6 @@ export declare const repositoryContext: AsyncLocalStorage<{

file: string;
importsUpdates: {
type: "add" | "remove";
import: string;
}[];
};

@@ -41,0 +50,0 @@ export declare const getRepositoryContext: () => {

@@ -107,3 +107,3 @@ "use strict";

for (const file of files) {
yield contexts_1.fileContext.run({ file: path.join(cwd, file) }, cb);
yield contexts_1.fileContext.run({ file: path.join(cwd, file), importsUpdates: [] }, cb);
}

@@ -110,0 +110,0 @@ });

@@ -66,5 +66,4 @@ "use strict";

}
console.log({ tmpDir });
if (yield (0, fs_1.isDirectory)(tmpDir)) {
console.log("Directory already exists, skipping clone, implement checkout");
console.log(`Directory ${tmpDir} already exists, skipping clone`);
const remoteDefaultBranch = yield getDefaultBranchFromRemote(repositoryUrl);

@@ -71,0 +70,0 @@ const remoteDefaultBranchHash = remoteDefaultBranch &&

@@ -1,6 +0,15 @@

import { PLazy } from "../PLazy.js";
import type { PLazy } from "../PLazy.js";
import { jsFiles } from "../jsFiles.js";
import { commit } from "./commit.js";
import { push } from "./push.js";
declare const helpers: {
/**
* Creates branch for current repository
*/
export declare function branchLogic(rawBranches: string | readonly string[]): PLazy<BranchHelpers> & BranchHelpers;
/**
* Creates branch for current repository
*/
export declare function branchLogic(rawBranches: string | readonly string[], callback: (helpers: BranchHelpers) => void | Promise<void>): PLazy<BranchHelpers> & BranchHelpers;
export declare const branch: typeof branchLogic;
declare const branchHelpers: {
jsFiles: typeof jsFiles;

@@ -10,5 +19,3 @@ commit: typeof commit;

};
type Helpers = typeof helpers;
export declare function branch(rawBranches: string | readonly string[]): PLazy<Helpers> & Helpers;
export declare function branch(rawBranches: string | readonly string[], callback: (helpers: Helpers) => void | Promise<void>): PLazy<Helpers> & Helpers;
type BranchHelpers = typeof branchHelpers;
export {};

@@ -12,4 +12,4 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.branch = void 0;
const PLazy_js_1 = require("../PLazy.js");
exports.branch = exports.branchLogic = void 0;
const engineHelpers_js_1 = require("src/engineHelpers.js");
const contexts_js_1 = require("../contexts.js");

@@ -21,37 +21,27 @@ const git_js_1 = require("../git.js");

const push_js_1 = require("./push.js");
const helpers = { jsFiles: jsFiles_js_1.jsFiles, commit: commit_js_1.commit, push: push_js_1.push };
function branch(rawBranches, callback) {
const innerParentContext = (0, contexts_js_1.getParentContext)();
const branches = (0, helpers_js_1.parseMultistring)(rawBranches);
const context = (cb) => __awaiter(this, void 0, void 0, function* () {
yield Promise.all(branches.map((branchName) => innerParentContext(() => __awaiter(this, void 0, void 0, function* () {
/**
* Creates branch for current repository
*/
function branchLogic(rawBranches, callback) {
return new engineHelpers_js_1.FunctionExecutor("branch")
.arguments(() => ({ branches: (0, helpers_js_1.parseMultistring)(rawBranches), callback }))
.helpers(branchHelpers)
.executor((next, self) => __awaiter(this, void 0, void 0, function* () {
const { branches } = self.getArguments();
yield Promise.all(branches.map((branchName) => __awaiter(this, void 0, void 0, function* () {
const repo = (0, contexts_js_1.getRepositoryContext)().repository;
yield (0, git_js_1.switchBranch)(branchName);
yield contexts_js_1.repositoryContext.run({ repository: repo, branch: branchName }, () => cb
? cb(callback ? helpers : (0, helpers_js_1.wrapHelpers)(helpers, context))
: Promise.resolve((0, helpers_js_1.wrapHelpers)(helpers, context)));
}), `${branch}`)));
return (0, helpers_js_1.wrapHelpers)(helpers, context);
});
const helpersWithContext = (0, helpers_js_1.wrapHelpers)(helpers, context);
const promise = new PLazy_js_1.PLazy((resolve, reject) => {
if (callback) {
const voidOrPromise = callback((0, helpers_js_1.wrapHelpers)(helpers, context));
if (voidOrPromise instanceof Promise) {
voidOrPromise
.then(() => resolve((0, helpers_js_1.wrapHelpers)(helpers, context)))
.catch(reject);
}
}
else {
context().then(resolve).catch(reject);
}
});
Object.keys(helpersWithContext).forEach((key) => {
// @ts-ignore
promise[key] = helpersWithContext[key];
});
return promise;
yield contexts_js_1.repositoryContext.run({ repository: repo, branch: branchName }, next);
})));
}))
.callback((self) => __awaiter(this, void 0, void 0, function* () {
const { callback } = self.getArguments();
yield (callback === null || callback === void 0 ? void 0 : callback(self.wrappedHelpers()));
}))
.return((self) => self.wrappedHelpers())
.run();
}
exports.branch = branch;
exports.branchLogic = branchLogic;
exports.branch = (0, engineHelpers_js_1.fnWrapper)("branch", branchLogic);
const branchHelpers = { jsFiles: jsFiles_js_1.jsFiles, commit: commit_js_1.commit, push: push_js_1.push };
//# sourceMappingURL=branch.js.map

@@ -1,15 +0,24 @@

import { PLazy } from "../PLazy.js";
import type { PLazy } from "../PLazy.js";
import { jsFiles } from "../jsFiles.js";
import { branch } from "./branch.js";
import { commit } from "./commit.js";
import { push } from "./push.js";
declare const helpers: {
/**
*
* @param rawRepositories
*/
export declare function cloneLogic(rawRepositories: string | readonly string[]): PLazy<CloneHelpers> & CloneHelpers;
/**
*
* @param rawRepositories
* @param callback
*/
export declare function cloneLogic(rawRepositories: string | readonly string[], callback: (helpers: CloneHelpers) => void | Promise<void>): PLazy<CloneHelpers> & CloneHelpers;
export declare const clone: typeof cloneLogic;
declare const cloneHelpers: {
jsFiles: typeof jsFiles;
branch: typeof branch;
branch: typeof import("./branch.js").branchLogic;
commit: typeof commit;
push: typeof push;
};
type Helpers = typeof helpers;
export declare function clone(rawRepositories: string | readonly string[]): PLazy<Helpers> & Helpers;
export declare function clone(rawRepositories: string | readonly string[], callback: (helpers: Helpers) => void | Promise<void>): PLazy<Helpers> & Helpers;
type CloneHelpers = typeof cloneHelpers;
export {};

@@ -12,6 +12,4 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.clone = void 0;
const node_crypto_1 = require("node:crypto");
const lodash_1 = require("lodash");
const PLazy_js_1 = require("../PLazy.js");
exports.clone = exports.cloneLogic = void 0;
const engineHelpers_js_1 = require("src/engineHelpers.js");
const contexts_js_1 = require("../contexts.js");

@@ -24,55 +22,32 @@ const git_js_1 = require("../git.js");

const push_js_1 = require("./push.js");
const helpers = { jsFiles: jsFiles_js_1.jsFiles, branch: branch_js_1.branch, commit: commit_js_1.commit, push: push_js_1.push };
function clone(rawRepositories, callback) {
const innerParentContext = (0, contexts_js_1.getParentContext)();
const repositories = (0, helpers_js_1.parseMultistring)(rawRepositories);
const localClone = (0, lodash_1.memoize)((repository, ...keys) => __awaiter(this, void 0, void 0, function* () {
const hash = (0, node_crypto_1.createHash)("sha1");
hash.update(JSON.stringify(keys));
return yield (0, git_js_1.cloneRepository)(repository, hash.digest("hex"));
}));
const context = (cb, info) => __awaiter(this, void 0, void 0, function* () {
yield innerParentContext(() => contexts_js_1.repositoriesContext.run({ repositories }, () => __awaiter(this, void 0, void 0, function* () {
yield Promise.all(repositories.map((repository, index) => contexts_js_1.cwdContext.run({ cwd: process.cwd() }, () => __awaiter(this, void 0, void 0, function* () {
const branch = yield localClone(repository, info, index);
yield contexts_js_1.repositoryContext.run({ repository, branch }, () => __awaiter(this, void 0, void 0, function* () {
if (callback) {
yield callback((0, helpers_js_1.wrapHelpers)(helpers, context));
}
// Remote execution should be here
// if (cb) {
// console.log('remote run:');
// console.log(getContextsSnapshot());
// console.log(cb.toString());
// }
yield (cb
? cb(callback ? helpers : (0, helpers_js_1.wrapHelpers)(helpers, context))
: Promise.resolve((0, helpers_js_1.wrapHelpers)(helpers, context)));
}));
}))));
return (0, helpers_js_1.wrapHelpers)(helpers, context);
})));
return (0, helpers_js_1.wrapHelpers)(helpers, context);
});
const helpersWithContext = (0, helpers_js_1.wrapHelpers)(helpers, context);
const promise = new PLazy_js_1.PLazy((resolve, reject) => {
if (callback) {
const voidOrPromise = callback((0, helpers_js_1.wrapHelpers)(helpers, context));
if (voidOrPromise instanceof Promise) {
voidOrPromise
.then(() => resolve((0, helpers_js_1.wrapHelpers)(helpers, context)))
.catch(reject);
}
}
else {
context().then(resolve).catch(reject);
}
});
Object.keys(helpersWithContext).forEach((key) => {
// @ts-ignore
promise[key] = helpersWithContext[key];
});
return promise;
/**
*
* @param rawRepositories
* @param callback
* @returns
*/
function cloneLogic(rawRepositories, callback) {
return new engineHelpers_js_1.FunctionExecutor("clone")
.arguments(() => ({
repositories: (0, helpers_js_1.parseMultistring)(rawRepositories),
callback,
}))
.helpers(cloneHelpers)
.executor((next, self) => __awaiter(this, void 0, void 0, function* () {
const { repositories } = self.getArguments();
yield Promise.all(repositories.map((repository, index) => contexts_js_1.cwdContext.run({ cwd: process.cwd() }, () => __awaiter(this, void 0, void 0, function* () {
const branch = yield (0, git_js_1.cloneRepository)(repository, String(index));
yield contexts_js_1.repositoryContext.run({ repository, branch }, next);
}))));
}))
.callback((self) => __awaiter(this, void 0, void 0, function* () {
const { callback } = self.getArguments();
yield (callback === null || callback === void 0 ? void 0 : callback(self.wrappedHelpers()));
}))
.return((self) => self.wrappedHelpers())
.run();
}
exports.clone = clone;
exports.cloneLogic = cloneLogic;
exports.clone = (0, engineHelpers_js_1.fnWrapper)("clone", cloneLogic);
const cloneHelpers = { jsFiles: jsFiles_js_1.jsFiles, branch: branch_js_1.branch, commit: commit_js_1.commit, push: push_js_1.push };
//# sourceMappingURL=clone.js.map

@@ -1,10 +0,8 @@

import { branch } from "./branch.js";
import { clone } from "./clone.js";
import { commit } from "./commit.js";
import { push } from "./push.js";
export declare const git: {
clone: typeof clone;
branch: typeof branch;
clone: typeof import("./clone.js").cloneLogic;
branch: typeof import("./branch.js").branchLogic;
push: typeof push;
commit: typeof commit;
};

@@ -14,3 +14,3 @@ export declare const clc: {

export declare const parseRepositories: (repos: string | Readonly<string[]> | ((...args: any[]) => Promise<void>)) => string[];
export declare const parseMultistring: (repos: string | readonly string[]) => string[];
export declare const parseMultistring: (repos: string | readonly string[], split?: RegExp) => string[];
export type MapChildren<Type extends Record<string, (...args: any[]) => any>> = {

@@ -17,0 +17,0 @@ [Property in keyof Type]: ReturnType<Type[Property]>;

@@ -66,6 +66,6 @@ "use strict";

exports.parseRepositories = parseRepositories;
const parseMultistring = (repos) => {
const parseMultistring = (repos, split = /[\n,; ]/) => {
if (typeof repos === "string") {
return repos
.split(/[\n,; ]/)
.split(split)
.map((repository) => repository.trim())

@@ -72,0 +72,0 @@ .filter(lodash_1.identity);

@@ -0,1 +1,2 @@

import * as contexts from "./contexts.js";
export * from "./contexts.js";

@@ -8,4 +9,4 @@ import { git } from "./git/git.js";

git: {
clone: typeof import("./git/clone.js").clone;
branch: typeof import("./git/branch.js").branch;
clone: typeof import("./git/clone.js").cloneLogic;
branch: typeof import("./git/branch.js").branchLogic;
push: typeof import("./git/push.js").push;

@@ -16,3 +17,4 @@ commit: typeof import("./git/commit.js").commit;

jsFiles: typeof jsFiles;
contexts: typeof contexts;
};
export type Api = typeof api;

@@ -13,2 +13,14 @@ "use strict";

}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {

@@ -19,2 +31,3 @@ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);

exports.api = exports.jsFiles = exports.astGrep = exports.git = void 0;
const contexts = __importStar(require("./contexts.js"));
__exportStar(require("./contexts.js"), exports);

@@ -27,3 +40,3 @@ const git_js_1 = require("./git/git.js");

Object.defineProperty(exports, "jsFiles", { enumerable: true, get: function () { return jsFiles_js_1.jsFiles; } });
exports.api = { git: git_js_1.git, astGrep: astGrep_js_1.astGrep, jsFiles: jsFiles_js_1.jsFiles };
exports.api = { git: git_js_1.git, astGrep: astGrep_js_1.astGrep, jsFiles: jsFiles_js_1.jsFiles, contexts };
//# sourceMappingURL=index.js.map
import { PLazy } from "./PLazy.js";
import { astGrep } from "./astGrep/astGrep.js";
import { getImports } from "./astGrep/getImports.js";
declare const helpers: {
astGrep: typeof astGrep;
getImports: typeof getImports;
addImport: (line: string) => void;
removeImport: (line: string) => void;
};

@@ -17,2 +21,3 @@ type Helpers = typeof helpers;

export declare function jsFiles(globs: string | readonly string[], callback: (helpers: Helpers) => void | Promise<void>): PLazy<Helpers> & Helpers;
export declare function jsFiles(callback: (helpers: Helpers) => void | Promise<void>): PLazy<Helpers> & Helpers;
export {};

@@ -37,11 +37,23 @@ "use strict";

const path = __importStar(require("node:path"));
const napi_1 = require("@ast-grep/napi");
const fg = __importStar(require("fast-glob"));
const PLazy_js_1 = require("./PLazy.js");
const astGrep_js_1 = require("./astGrep/astGrep.js");
const getImports_js_1 = require("./astGrep/getImports.js");
const contexts_js_1 = require("./contexts.js");
const helpers_js_1 = require("./helpers.js");
const helpers = { astGrep: astGrep_js_1.astGrep };
function jsFiles(rawGlobs, callback) {
const helpers = {
astGrep: astGrep_js_1.astGrep,
getImports: getImports_js_1.getImports,
addImport: (line) => {
(0, contexts_js_1.getFileContext)().importsUpdates.push({ type: "add", import: line });
},
removeImport: (line) => {
(0, contexts_js_1.getFileContext)().importsUpdates.push({ type: "remove", import: line });
},
};
function jsFiles(rawGlobs, maybeCallback) {
const innerParentContext = (0, contexts_js_1.getParentContext)();
const globs = (0, helpers_js_1.parseMultistring)(rawGlobs);
const globs = (0, helpers_js_1.parseMultistring)(typeof rawGlobs === "function" ? "**/*.{js,jsx,ts,tsx,cjs,mjs}" : rawGlobs, /[\n; ]/);
const callback = typeof rawGlobs === "function" ? rawGlobs : maybeCallback;
const context = (cb) => __awaiter(this, void 0, void 0, function* () {

@@ -62,3 +74,3 @@ yield innerParentContext(() => Promise.all(globs.map((glob) => __awaiter(this, void 0, void 0, function* () {

for (const file of files) {
yield contexts_js_1.fileContext.run({ file: path.join(cwd, file) }, (...args) => {
yield contexts_js_1.fileContext.run({ file: path.join(cwd, file), importsUpdates: [] }, (...args) => __awaiter(this, void 0, void 0, function* () {
// Remote execution should be here

@@ -70,4 +82,60 @@ // if (cb) {

// }
return cb(...args);
}, helpers);
const result = yield cb(...args);
const { importsUpdates } = (0, contexts_js_1.getFileContext)();
const getImportInfo = (node) => {
var _a;
return ({
from: (_a = node.getMatch("FROM")) === null || _a === void 0 ? void 0 : _a.text(),
imports: node
.getMultipleMatches("IMPORTS")
.filter((n) => n.kind() !== ",")
.map((n) => n.text()),
});
};
const importRule = {
rule: {
any: [
{ pattern: "import { $$$IMPORTS } from '$FROM'" },
{ pattern: 'import { $$$IMPORTS } from "$FROM"' },
],
},
};
if (importsUpdates.length) {
for (const { type, import: line } of importsUpdates) {
const namedImportsToChange = napi_1.ts
.parse(line)
.root()
.findAll(importRule);
for (const node of namedImportsToChange) {
const importChange = getImportInfo(node);
yield (0, astGrep_js_1.astGrep)(importRule).replace(({ getNode }) => {
const currentImports = getImportInfo(getNode());
let modified = false;
if (currentImports.from === importChange.from) {
for (const namedImport of importChange.imports) {
if (type === "add") {
if (!currentImports.imports.includes(namedImport)) {
modified = true;
currentImports.imports.push(namedImport);
}
}
else if (type === "remove") {
if (currentImports.imports.includes(namedImport)) {
modified = true;
currentImports.imports =
currentImports.imports.filter((imp) => imp !== namedImport);
}
}
}
}
if (modified) {
return `import { ${currentImports.imports.join(", ")} } from "${currentImports.from}"`;
}
return undefined;
});
}
}
}
return result;
}), helpers);
}

@@ -74,0 +142,0 @@ }

{
"name": "@codemod.com/workflow",
"author": "Codemod, Inc.",
"version": "0.0.1",
"version": "0.0.2",
"description": "Workflow Engine for Codemod",

@@ -27,2 +27,3 @@ "main": "./dist/index.js",

"dependencies": {
"@ast-grep/cli": "^0.22.3",
"@ast-grep/napi": "^0.22.3",

@@ -32,2 +33,3 @@ "@sindresorhus/slugify": "^2.2.1",

"filenamify": "^6.0.0",
"magic-string": "^0.30.10",
"simple-git": "^3.24.0",

@@ -34,0 +36,0 @@ "tree-kill": "^1.2.2",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc