Skip to content

Commit df45be4

Browse files
authored
Merge pull request #15 from va4ok/v_0_1_6
V 0 1 6
2 parents 87da24e + 607519e commit df45be4

4 files changed

Lines changed: 76 additions & 12 deletions

File tree

builder/build.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const os = require('os');
55
const minimist = require('minimist');
66

77
const inlineCss = require('./inline-css');
8+
const fileHelper = require('./file-helper');
89
const getMeta = require('./get-meta');
910
const config = require('./config');
1011
const version = require('./version');
@@ -45,23 +46,40 @@ Version: \x1b[36m${newversion}\x1b[0m`);
4546
}
4647
}
4748

49+
function getExistingFilePath(filePath, normalizedFilePath, parentPath) {
50+
if (fs.existsSync(filePath)) {
51+
return filePath;
52+
}
53+
54+
if (fs.existsSync(normalizedFilePath)) {
55+
return normalizedFilePath;
56+
}
57+
58+
throw new Error(`${parentPath} tries to import unreachable file ${filePath}`);
59+
}
60+
4861
function buildTree(filePath, parentPath) {
49-
// Get file name
62+
// Get full file name
5063
if (parentPath) {
5164
filePath = path.join(parentPath, '..', filePath);
5265
}
5366

54-
if (visited.includes(filePath)) {
67+
const normalizedFilePath = fileHelper.normalizeFileName(filePath);
68+
69+
if (visited.includes(filePath) || visited.includes(normalizedFilePath)) {
70+
// file processed
5571
return;
5672
}
5773

58-
// Put file name into visited
59-
visited.push(filePath);
74+
filePath = getExistingFilePath(filePath, normalizedFilePath, parentPath);
6075

6176
// Open file
62-
const file = fs.readFileSync(filePath).toString();
6377
const importRegex = /^[\t\r ]*import.+['"];$/gm;
6478
const imports = [];
79+
const file = fs.readFileSync(filePath).toString();
80+
81+
// Put file name into visited
82+
visited.push(filePath);
6583

6684
// Get all imports
6785
let matches;
@@ -75,7 +93,7 @@ function buildTree(filePath, parentPath) {
7593
});
7694

7795
if (/\.css$/g.test(filePath)) {
78-
css.push({ file, filePath: filePath.split('\\').join('/') });
96+
css.push({file, filePath: filePath.split('\\').join('/')});
7997
} else {
8098
files.push({
8199
file: file
@@ -99,11 +117,13 @@ function getOutFile(addFilePathComments) {
99117
console.log('\x1b[33m%s\x1b[0m', 'Concat js files');
100118

101119
files.forEach(file => {
102-
console.log(`${file.filePath.replace(/^\.\//g, '')}`);
120+
const filePath = file.filePath.replace(/^\.\//g, '');
121+
122+
console.log(`${filePath}`);
103123
out += os.EOL + os.EOL;
104124

105125
if (addFilePathComments) {
106-
out += `// ${file.filePath.replace(/^\.\//g, '')}${os.EOL}`;
126+
out += `// ${filePath}${os.EOL}`;
107127
}
108128

109129
out += file.file;

builder/file-helper.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const extensions = [
4+
'js',
5+
'css'
6+
];
7+
8+
function isContainsExtension(filename, ext) {
9+
// we need check if extension with dot are last characters in filename
10+
// not sure if it is a good solution but it doesn't create any regexp
11+
const dotExt = '.' + ext;
12+
const extLength = dotExt.length;
13+
const offset = filename.length - extLength;
14+
15+
return filename.indexOf(dotExt, offset) !== -1;
16+
}
17+
18+
function isContainsExtensions(filename, extArr) {
19+
const filenameLowCase = filename.toLowerCase();
20+
let result = false;
21+
22+
if (Array.isArray(extArr)) {
23+
extArr.forEach(ext => {
24+
// if result true - we already found extension
25+
if (!result) {
26+
result = isContainsExtension(filenameLowCase, ext);
27+
}
28+
});
29+
} else {
30+
result = isContainsExtension(filenameLowCase, extArr.toString());
31+
}
32+
33+
return result;
34+
}
35+
36+
function normalizeFileName(filePath) {
37+
if (isContainsExtensions(filePath, extensions)) {
38+
return filePath;
39+
}
40+
41+
return filePath + '.js';
42+
}
43+
44+
module.exports = {normalizeFileName};

example/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Notificator } from "./notificator/notificator.js";
2-
import { SayHello } from "./say-hello/say-hello.js";
2+
import { SayHello } from "./say-hello/say-hello";
33

44
(function () {
55
Notificator.notify('creating John and Mark');
@@ -11,4 +11,4 @@ import { SayHello } from "./say-hello/say-hello.js";
1111

1212
greetJohn.now();
1313
greetMark.now();
14-
})();
14+
})();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "userscript-builder",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Tool for building userscript for teampermonkey.",
55
"main": "builder/build.js",
66
"bin": "./bin/builder.js",
@@ -45,4 +45,4 @@
4545
"grant": "none"
4646
}
4747
}
48-
}
48+
}

0 commit comments

Comments
 (0)