Can be a repository or array of repositories which you need to clone.
By default shallow cloning is performed with --depth 1 --single-branch. You can specify branch to clone specific (not default branch).
If you want to disable shallow clone, you can provide it with extended configuration.
A callback which will be executed for each repository. First argument is an object with subcommands. It can be destructured to get access to subcommands.
Filters out directories using glob.
By default all the directories will be found.
When iterating over directories - new current working directory context is set. It means that all the commands executed inside callback will be executed in the context of the directory.
Copy
import { dirs } from '@codemod.com/workflow'await dirs()
A callback which will be executed for each directory. First argument is an object with subcommands. It can be destructured to get access to subcommands.
import type { SgNode } from '@ast-grep/napi'interface CallbackFunctionHelpers { addImport: (imports: string) => void, removeImport: (imports: string) => void, astGrep: // see pattern matching, jscodeshift: // see jscodeshift,}type CallbackFunction = (helpers: CallbackFunctionHelpers) => Promise<void> | void
Allows to use JavaScript/TypeScript specific subcommands and helpers. Inside callback you can use addImport and removeImport to add or remove imports, astGrep to search for code patterns and jscodeshift to transform code.
jsFam is always should be used together with files command. It automatically adds glob to find all js/ts files, so calling files().jsFam() is equivalent to files('**/*.{js,jsx,ts,tsx,cjs,mjs}').jsFam().
Callback helpers:
addImport(imports) - Accepts code with imports and automatically merges file imports with provided imports.
removeImport(imports) - Accepts code with imports and automatically removes provided imports from file imports.
Search part of the code (single AST node) using ast-grep and iterate over found nodes in the callback or returned commands.
Creates separate context for each found node, which could be reused later.
Copy
import { astGrep } from '@codemod.com/workflow'await astGrep('console.log($$$A)') .replace('console.error($$$A)')
callback
Copy
import { git } from '@codemod.com/workflow'await git .clone('repository-to-clone') .files('**/*.{jsx,tsx}') .jsFam() /** * For string patterns, by default * strictness `relaxed` is used, * so comments and non-significant * syntax constructs will be ignored. * Possible matches: * - import React from 'react' * - import React from "react" */ .astGrep('import React from "react"', async ({ replace, map }) => { // remove import statement await replace('') const foundCode = await map(({ getNode }) => getNode().text()) console.log(`Found code:\n${foundCode.join('\n')}`)})
String pattern, e.g. console.log($$$A); by default relaxedstrictness algorithm is used, meaning that comments and non-significant syntax constructs (like single and double quotes for JavaScript) will be ignored.
Object pattern, using NapiConfig, which will be passed as is to ast-grep engine.
Template literal, using YAML format inside. It is a syntax sugar for NapiConfig object, so you can copy rules from ast-grep playground and paste them here.
Object pattern, using AstGrepAPI. In this case request is sent to ast-grep CLI and you can use all its features, like fix. But there is no programmatic access to the results, it is one-way operation. It has similar syntax to NapiConfig, but with additional id field.
A callback which will be executed for each repository. First argument is an object with subcommands. It can be destructured to get access to subcommands.