Skip to content

Commit 49ca70c

Browse files
committed
Implement imports rewriting
1 parent bacbff6 commit 49ca70c

5 files changed

Lines changed: 104 additions & 2 deletions

File tree

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
22
"env": {
3-
"node": true
3+
"node": true,
4+
"es6": true
45
},
56
"extends": "eslint:recommended",
67
"rules": {

lib/models/file-info.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ var path = require('path');
22
var CoreObject = require('core-object');
33
var fse = require('fs-extra');
44
var existsSync = require('exists-sync');
5+
var jscodeshift = require('jscodeshift');
56
var isTypeInSingleTypeCollection = require('../utils/is-type-in-single-type-collection');
67
var defaultTypeForCollection = require('../utils/default-type-for-collection');
78
var calculateCollectionInfo = require('../utils/calculate-collection-info');
9+
var importDeclarationsTransform = require('../transforms/import-declarations');
810

911
var FileInfo = CoreObject.extend({
1012
type: 'FileInfo',
@@ -95,7 +97,33 @@ var FileInfo = CoreObject.extend({
9597
this._fileContents = fse.readFileSync(fullPath, { encoding: 'utf8' });
9698
},
9799

100+
updateImports: function() {
101+
if (this.ext !== '.js' || !this._fileContents) { return; } // only process JavaScript files
102+
103+
var appName = this.options.projectName;
104+
105+
try {
106+
var newContents = importDeclarationsTransform(
107+
{ source: this._fileContents,
108+
fileInfo: this,
109+
appName: appName,
110+
fileInfos: this._fileInfoCollection._fileInfos
111+
},
112+
{ jscodeshift });
113+
114+
var fullPath = path.join(this.projectRoot, this.sourceRelativePath);
115+
116+
fse.writeFileSync(fullPath, newContents, { encoding: 'utf-8' });
117+
this._fileContents = newContents;
118+
} catch(e) {
119+
// eslint-disable-next-line no-console
120+
console.log('error parsing file `' + this.sourceRelativePath + '` failed to apply codeshift. Possible invalid JS file. Returning original file unchanged. error: ' + e.message);
121+
}
122+
},
123+
98124
repopulate: function() {
125+
this.updateImports();
126+
99127
var inComponentsCollection = this.collection === 'components';
100128
var renderableName = path.join(this.namespace, this.name);
101129

@@ -135,7 +163,7 @@ var FileInfo = CoreObject.extend({
135163
var fullPath = path.join(this.projectRoot, this.sourceRelativePath);
136164

137165
fse.writeFileSync(fullPath, newContents, { encoding: 'utf-8' });
138-
this._fileContents = fse.readFileSync(fullPath, { encoding: 'utf8' });
166+
this._fileContents = newContents;
139167
},
140168

141169
shouldUseDotFormNaming: function() {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var path_utils = require('../utils/path');
2+
3+
function transformer(file, api) {
4+
var j = api.jscodeshift;
5+
6+
return j(file.source)
7+
.find(j.ImportDeclaration)
8+
.find(j.Literal)
9+
.forEach(function(path) {
10+
var importPath = path.value.value + '.js';
11+
var appName = file.appName;
12+
13+
if (!appName) {
14+
// skip import transforms if appName is not set
15+
return;
16+
}
17+
18+
var relative = path_utils.isRelative(importPath);
19+
if (relative) {
20+
importPath = path_utils.makeAbsolute(appName + '/' + file.fileInfo.sourceRelativePath, importPath);
21+
} else {
22+
// check if import path starts with appName and add /app
23+
if (importPath.startsWith(appName + '/') && !importPath.startsWith(appName + '/app/')) {
24+
importPath = appName + '/app' + importPath.substring(appName.length);
25+
}
26+
}
27+
28+
var targetFileInfo = file.fileInfos.find(function(f) {
29+
return (appName + '/' + f.sourceRelativePath) === importPath;
30+
});
31+
32+
if (!targetFileInfo) {
33+
// TODO error message
34+
return;
35+
}
36+
37+
var newImportPath = appName + '/' + targetFileInfo.destRelativePath;
38+
if (relative) {
39+
newImportPath = path_utils.makeRelative(file.fileInfo.destRelativePath, targetFileInfo.destRelativePath);
40+
}
41+
// remove extension
42+
newImportPath = newImportPath.slice(0, -targetFileInfo.ext.length);
43+
j(path).replaceWith(j.literal(newImportPath));
44+
})
45+
.toSource();
46+
47+
}
48+
49+
module.exports = transformer;

lib/utils/path.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var p = require('path');
2+
3+
function isRelative(path) {
4+
return path[0] === '.';
5+
}
6+
7+
function makeRelative(from, to) {
8+
var path = p.relative(p.dirname(from), to);
9+
if (!isRelative(path)) {
10+
path = './' + path;
11+
}
12+
return path;
13+
}
14+
15+
function makeAbsolute(base, path) {
16+
return p.resolve(p.dirname('/' + base), path).substring(1);
17+
}
18+
19+
module.exports = {
20+
isRelative,
21+
makeRelative,
22+
makeAbsolute
23+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"assert-diff": "^1.0.1",
1818
"espower-loader": "^1.0.0",
1919
"fixturify": "^0.2.0",
20+
"jscodeshift": "^0.3.30",
2021
"mocha": "^2.4.5",
2122
"mocha-eslint": "^2.0.2",
2223
"power-assert": "^1.3.1",

0 commit comments

Comments
 (0)