Skip to content

Commit e349bd3

Browse files
committed
updates per pr & some cleanup
1 parent 04f8eba commit e349bd3

5 files changed

Lines changed: 48 additions & 120 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ A collection of codemod's for ember-cli-htmlbars-inline-precompile-codemod.
1010
To run a specific codemod from this project, you would run the following:
1111

1212
```
13-
npx ember-cli-htmlbars-inline-precompile-codemod <TRANSFORM NAME> path/of/files/ or/some**/*glob.js
13+
npx ember-cli-htmlbars-inline-precompile-codemod path/of/files/ or/some**/*glob.js
1414
1515
# or
1616
1717
yarn global add ember-cli-htmlbars-inline-precompile-codemod
18-
ember-cli-htmlbars-inline-precompile-codemod <TRANSFORM NAME> path/of/files/ or/some**/*glob.js
18+
ember-cli-htmlbars-inline-precompile-codemod path/of/files/ or/some**/*
1919
```
2020

2121
## Transforms

bin/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
require('codemod-cli').runTransform(
55
__dirname,
6-
process.argv[2] /* transform name */,
7-
process.argv.slice(3) /* paths or globs */
6+
'convert-htmlbars-import' /* transform name */,
7+
process.argv.slice(2) /* paths or globs */
88
);

transforms/convert-htmlbars-import/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
## Usage
55

66
```
7-
npx ember-cli-htmlbars-inline-precompile-codemod convert-htmlbars-import path/of/files/ or/some**/*glob.js
7+
npx ember-cli-htmlbars-inline-precompile-codemod path/of/files/ or/some**/*glob.js
88
99
# or
1010
1111
yarn global add ember-cli-htmlbars-inline-precompile-codemod
12-
ember-cli-htmlbars-inline-precompile-codemod convert-htmlbars-import path/of/files/ or/some**/*glob.js
12+
ember-cli-htmlbars-inline-precompile-codemod path/of/files/ or/some**/*
1313
```
1414

1515
## Input / Output
Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
11
const { getParser } = require('codemod-cli').jscodeshift;
2-
const { addImportStatement, writeImportStatements } = require('../utils');
3-
// const { getOptions } = require('codemod-cli');
42

53
module.exports = function transformer(file, api) {
64
const j = getParser(api);
7-
85
const root = j(file.source);
96

10-
addImportStatement(['hbs']);
11-
writeImportStatements(j, root);
7+
writeImportStatement(j, root);
128

139
return root.toSource({ quote: 'single' });
1410
};
11+
12+
function writeImportStatement(j, root) {
13+
let body = root.get().value.program.body;
14+
15+
// finding all 'htmlbars-inline-precompile' import statements
16+
const inlinePrecompileImportStatement = root.find(j.ImportDeclaration, {
17+
source: {
18+
value: 'htmlbars-inline-precompile',
19+
},
20+
});
21+
22+
// if any imports from 'htmlbars-inline-precompile' exists, remove it and replace with a new import
23+
if (inlinePrecompileImportStatement.length !== 0) {
24+
inlinePrecompileImportStatement.remove();
25+
26+
// setting up 'hbs' as an identifier for import statement
27+
const hbs = 'hbs';
28+
const hbsAsIdentifier = j.identifier(hbs);
29+
const variableId = j.importSpecifier(hbsAsIdentifier);
30+
31+
// finding all 'ember-cli-htmlbars' import statements
32+
const emberCliImportStatement = root.find(j.ImportDeclaration, {
33+
source: {
34+
value: 'ember-cli-htmlbars',
35+
},
36+
});
37+
38+
// if no imports from 'ember-cli-htmlbars' exists, write import { hbs } from 'ember-cli-htmlbars';
39+
if (emberCliImportStatement.length === 0) {
40+
const importStatement = j.importDeclaration([variableId], j.literal('ember-cli-htmlbars'));
41+
body.unshift(importStatement);
42+
}
43+
// if any imports from 'ember-cli-htmlbars' already exists, include hbs
44+
else {
45+
let existingSpecifiers = emberCliImportStatement.get('specifiers');
46+
if (existingSpecifiers.filter(exSp => exSp.value.imported.name === hbs).length === 0) {
47+
existingSpecifiers.push(variableId);
48+
}
49+
}
50+
}
51+
}

transforms/utils.js

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)