Skip to content

Commit

Permalink
🏗 Update how to execute jscodeshift (#32996)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanorozco committed Mar 3, 2021
1 parent 4fefdf8 commit 3aa6c2c
Show file tree
Hide file tree
Showing 7 changed files with 475 additions and 22 deletions.
24 changes: 5 additions & 19 deletions build-system/tasks/sweep-experiments/index.js
Expand Up @@ -19,6 +19,7 @@ const path = require('path');
const tempy = require('tempy');
const {cyan, magenta, yellow} = require('kleur/colors');
const {getOutput} = require('../../common/process');
const {jscodeshift} = require('../../test-configs/jscodeshift');
const {log} = require('../../common/logging');
const {readJsonSync, writeFileSync} = require('fs-extra');

Expand Down Expand Up @@ -92,30 +93,14 @@ const getModifiedSourceFiles = (fromHash) =>
(file) => !globalWritablePaths.includes(file)
);

/**
* Runs a jscodeshift transform under this directory.
* @param {string} transform
* @param {Array<string>=} args
* @return {string}
*/
const jscodeshift = (transform, args = []) =>
getStdoutThrowOnError(
[
'npx jscodeshift',
'--parser babylon',
`--parser-config ${__dirname}/jscodeshift/parser-config.json`,
`--transform ${__dirname}/jscodeshift/${transform}`,
...args,
].join(' ')
);

/**
* @param {string} id
* @param {string} experimentsRemovedJson
* @return {Array<string>} modified files
*/
function removeFromExperimentsConfig(id, experimentsRemovedJson) {
jscodeshift('remove-experiment-config.js', [
jscodeshift([
`--transform ${__dirname}/jscodeshift/remove-experiment-config.js`,
`--experimentId=${id}`,
`--experimentsRemovedJson=${experimentsRemovedJson}`,
experimentsConfigPath,
Expand Down Expand Up @@ -154,7 +139,8 @@ function removeFromRuntimeSource(id, percentage) {
id
);
if (possiblyModifiedSourceFiles.length > 0) {
jscodeshift('remove-experiment-runtime.js', [
jscodeshift([
`--transform ${__dirname}/jscodeshift/remove-experiment-runtime.js`,
`--isExperimentOnLaunched=${percentage}`,
`--isExperimentOnExperiment=${id}`,
...possiblyModifiedSourceFiles,
Expand Down

This file was deleted.

7 changes: 7 additions & 0 deletions build-system/test-configs/jscodeshift/README.md
@@ -0,0 +1,7 @@
# [jscodeshift](https://github.com/facebook/jscodeshift)

This directory holds shared configuration and utils for **jscodeshift**.

- [`parser-config.json`](./parser-config.json) is mostly taken from [the default babylon configuration](https://github.com/facebook/jscodeshift/blob/48f5d6d6e5e769639b958f1a955c83c68157a5fa/parser/babylon.js).

Different build tasks can hold local transforms, [you may search each of them.](https://github.com/ampproject/amphtml/search?l=JavaScript&q=jscodeshift)
33 changes: 33 additions & 0 deletions build-system/test-configs/jscodeshift/index.js
@@ -0,0 +1,33 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const {getOutput} = require('../../common/exec');

/**
* @param {Array<string>} args
* @return {string}
*/
const jscodeshift = (args = []) =>
getOutput(
[
'npx jscodeshift',
'--parser babylon',
`--parser-config ${__dirname}/parser-config.json`,
...args,
].join(' ')
);

module.exports = {jscodeshift};
33 changes: 33 additions & 0 deletions build-system/test-configs/jscodeshift/parser-config.json
@@ -0,0 +1,33 @@
{
"sourceType": "module",
"allowImportExportEverywhere": true,
"allowReturnOutsideFunction": true,
"startLine": 1,
"tokens": true,
"plugins": [
["flow", {"all": true}],
"flowComments",
"jsx",
"asyncGenerators",
"bigInt",
"classProperties",
"classPrivateProperties",
"classPrivateMethods",
["decorators", {"decoratorsBeforeExport": false}],
"doExpressions",
"dynamicImport",
"exportDefaultFrom",
"exportNamespaceFrom",
"functionBind",
"functionSent",
"importMeta",
"logicalAssignment",
"nullishCoalescingOperator",
"numericSeparator",
"objectRestSpread",
"optionalCatchBinding",
"optionalChaining",
["pipelineOperator", {"proposal": "minimal"}],
"throwExpressions"
]
}

0 comments on commit 3aa6c2c

Please sign in to comment.