{"id":13804403,"url":"https://github.com/dyfactor/dyfactor","last_synced_at":"2026-03-10T21:33:42.219Z","repository":{"id":66079376,"uuid":"132190142","full_name":"dyfactor/dyfactor","owner":"dyfactor","description":"A platform for running codemods based on runtime information","archived":false,"fork":false,"pushed_at":"2021-10-19T07:27:21.000Z","size":222,"stargazers_count":8,"open_issues_count":3,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-13T17:37:17.440Z","etag":null,"topics":["refactoring","runtime-analysis","static-analysis"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dyfactor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-05-04T21:19:40.000Z","updated_at":"2019-02-04T01:03:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"47bf02d8-d1e5-4b21-babb-51c56f40a7c6","html_url":"https://github.com/dyfactor/dyfactor","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/dyfactor/dyfactor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dyfactor%2Fdyfactor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dyfactor%2Fdyfactor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dyfactor%2Fdyfactor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dyfactor%2Fdyfactor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dyfactor","download_url":"https://codeload.github.com/dyfactor/dyfactor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dyfactor%2Fdyfactor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30355732,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T15:55:29.454Z","status":"ssl_error","status_checked_at":"2026-03-10T15:54:58.440Z","response_time":106,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["refactoring","runtime-analysis","static-analysis"],"created_at":"2024-08-04T01:00:47.158Z","updated_at":"2026-03-10T21:33:42.182Z","avatar_url":"https://github.com/dyfactor.png","language":"TypeScript","readme":"![Dyfactor](./logo.png)\n\n[![Build Status](https://travis-ci.org/dyfactor/dyfactor.svg?branch=master)](https://travis-ci.org/dyfactor/dyfactor)\n\nDyfactor is platform for writing and executing profile-guided codemods. By combining runtime information and static analysis, applications are able migrate codebases to new APIs at a much quicker pace.\n\n**See Dyfactor In Action**\n\n\u003cdiv style=\"text-align:center\"\u003e\n  \u003ca style=\"text-align:center\" href=\"https://youtu.be/D0d3rMp-T9I\"\u003e\u003cimg alt=\"Dyfactor Video\" src=\"https://img.youtube.com/vi/D0d3rMp-T9I/0.jpg\" /\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n# Install\n\n```bash\n$ yarn global add dyfactor\n```\n\n# Usage\n\n```bash\n$ yarn dyfactor --help\n\n  Usage: dyfactor [options] [command]\n\n  Options:\n\n    -h, --help    output usage information\n\n  Commands:\n\n    init          Initializes dyfactor in your project\n    run           Runs a plugin against code\n    list-plugins  Lists the available plugins\n    help [cmd]    display help for [cmd]\n```\n\n# Why Dyfactor?\n\nTools like JSCodeShift and Babel are excellent tools for migrating code from one API to another due to the fact they rely on static analysis. However, it is completely possible for parts of an application to be relying on runtime information to determine execution. In other cases you may have custom DSLs like template languages that don't have the same static analysis guarantees as JavaScript does. Migrating this type of code typically requires a great deal of developer intervention to sort out what is actually happening. Dyfactor's goal is to shorten this crevasse of understanding.\n\n## A Quick Example\n\nIn Ember's template layer developers can invoke components with arguments. They also can look at local values on the backing class. A template for a component may look like the following.\n\n```hbs\n\u003ch1\u003e{{firstName}} {{lastName}}\u003c/h1\u003e\n\u003ch2\u003eInfo\u003c/h2\u003e\n\u003cul\u003e\n  \u003cli\u003ePosts: {{postCount}}\u003c/li\u003e\n  \u003cli\u003eShares: {{shareCount}}\u003c/li\u003e\n\u003c/ul\u003e\n```\n\nWhile this template is declarative, it's not obvious if any of the `MustacheExpressions` (curlies) were arguments to the component or if they are local values on the component class. The only way to know is to go look at the invocation of the component. In doing so, we find that `firstName` and `lastName` are passed in as arguments.\n\n```hbs\n{{post-info firstName=fName lastName=lName}}\n```\n\nThe Ember Core has recognized this is extremely problematic as an application grows, so they have allowed arguments to be pre-fixed with `@` and locals to be prefixed with `this.`. The issue is that migrating all the templates in a project would take too long because it requires developers to go separate these things out.\n\nThis is where Dyfactor comes in. By writing a [Dynamic Plugin](#dynamic-plugins), we can instrument the application in such a way that allows us to know how these symbols are being resolved at runtime. From there, we can use that information to go manually migrate the code or let Dyfactor attempt to do the migration for us.\n\n# How Does It Work?\n\nAt a high level, Dyfactor is a runner and plugin system. It currently supports two types of plugins: Static Plugins and Dynamic Plugins.\n\n## Static Plugins\n\nA Static Plugin is meant to be used to perform codemods that only require static analysis and is only single phased. These should be thought of as a light wrapper around a codemod you would write with jscodeshift or Babel.\n\n### Plugin Interface\n\n```ts\ninterface StaticPlugin {\n  /**\n   * An array containing all recursive files and directories\n   * under a given directory\n   **/\n  inputs: string[];\n  /**\n   * Hook called by the runner where codemod is implimented\n   */\n  modify(): void;\n}\n```\n\n### Example:\n\n```js\nimport { StaticPlugin } from 'dyfactor';\nimport * as fs from 'fs';\nimport * as recast from 'recast';\n\nfunction filesOnly(path) {\n  return path.charAt(path.length - 1) !== '/';\n}\n\nexport default class extends StaticPlugin {\n  modify() {\n    this.inputs.filter(filesOnly).forEach(input =\u003e {\n      let content = fs.readFileSync(input, 'utf8');\n      let ast = recast.parse(content);\n      let add = ast.program.body[0];\n      let { builders: b } = recast.types;\n\n      ast.program.body[0] = b.variableDeclaration('var', [\n        b.variableDeclarator(\n          add.id,\n          b.functionExpression(\n            null, // Anonymize the function expression.\n            add.params,\n            add.body\n          )\n        )\n      ]);\n\n      add.params.push(add.params.shift());\n      let output = recast.prettyPrint(ast, { tabWidth: 2 }).code;\n      fs.writeFileSync(input, output);\n    });\n  }\n}\n```\n\n## Dynamic Plugins\n\nA Dynamic Plugin is two-phased. The first phase allows you to safely instrument an application to collect that runtime telemetry data. The instrumented application is then booted with Puppeteer to collect the data. The second phase is responsible for introspecting the runtime telemetry data and applying codemods based on that data. It's important to note that Dynamic Plugins can be run as single phased plugins just to produce the runtime telemetry and write it to disk.\n\n### Plugin Interface\n\n```ts\ninterface DynamicPlugin {\n  /**\n   * An array containing all recursive files and directories\n   * under a given directory\n   **/\n  inputs: string[];\n  /**\n   * Hook called by the runner to instrument the application\n   */\n  instrument(): void;\n\n  /**\n   * Hook called by the runner to apply codemods based on the telemtry\n   */\n  modify(telemetry: Telemetry): void;\n}\n\ninterface Telemetry {\n  data: any;\n}\n```\n\n### Example\n\n```js\nimport { DynamicPlugin, TelemetryBuilder } from 'dyfactor';\nimport * as fs from 'fs';\nimport { preprocess, print } from '@glimmer/syntax';\nimport { transform } from 'babel-core';\n\nfunction filesOnly(path) {\n  return path.charAt(path.length - 1) !== '/';\n}\n\nfunction instrumentCreate(babel) {\n  const { types: t } = babel;\n  let ident;\n  let t = new TelemetryBuilder();\n  let template = babel.template(`\n    IDENT.reopenClass({\n      create(injections) {\n        let instance = this._super(injections);\n        ${t.preamble()}\n        ${t.conditionallyAdd(\n          'instance._debugContainerKey',\n          () =\u003e {\n            return `Object.keys(injections.attrs).forEach((arg) =\u003e {\n            if (!${t.path('instance._debugContainerKey')}.contains(arg)) {\n              ${t.path('instance._debugContainerKey')}.push(arg);\n            }\n          });`;\n          },\n          () =\u003e {\n            return `${t.path('instance._debugContainerKey')} = Object.keys(injections.attrs);`;\n          }\n        )}\n        return instance;\n      }\n    });\n  `);\n  return {\n    name: 'instrument-create',\n    visitor: {\n      Program: {\n        enter(p) {\n          ident = p.scope.generateUidIdentifier('refactor');\n        },\n        exit(p) {\n          let body = p.node.body;\n          let ast = template({ IDENT: ident });\n          body.push(ast, t.exportDefaultDeclaration(ident));\n        }\n      },\n      ExportDefaultDeclaration(p) {\n        let declaration = p.node.declaration;\n        let declarator = t.variableDeclarator(ident, declaration);\n        let varDecl = t.variableDeclaration('const', [declarator]);\n        p.replaceWith(varDecl);\n      }\n    }\n  };\n}\n\nexport default class extends DynamicPlugin {\n  instrument() {\n    this.inputs.filter(filesOnly).forEach(input =\u003e {\n      let code = fs.readFileSync(input, 'utf8');\n      let content = transform(code, {\n        plugins: [instrumentCreate]\n      });\n\n      fs.writeFileSync(input, content.code);\n    });\n  }\n\n  modify(telemetry) {\n    telemetry.data.forEach(components =\u003e {\n      Object.keys(components).forEach(component =\u003e {\n        let templatePath = this.templateFor(component);\n        let template = fs.readFileSync(templatePath, 'utf8');\n        let ast = preprocess(template, {\n          plugins: {\n            ast: [toArgs(components[component])]\n          }\n        });\n        fs.writeFileSync(templatePath, print(ast));\n      });\n    });\n  }\n\n  templateFor(fullName: string) {\n    let [, name] = fullName.split(':');\n    return this.inputs.find(input =\u003e input.includes(`templates/components/${name}`));\n  }\n}\n```\n","funding_links":[],"categories":["Packages"],"sub_categories":["AST"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdyfactor%2Fdyfactor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdyfactor%2Fdyfactor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdyfactor%2Fdyfactor/lists"}