forked from aruberto/react-foundation-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-global-scoped-components.js
61 lines (50 loc) · 1.87 KB
/
create-global-scoped-components.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable no-console */
const path = require('path');
const fs = require('fs-extra');
const through2 = require('through2');
const s = require('underscore.string');
const libPath = path.join(__dirname, 'lib');
const globalPath = path.join(libPath, 'global');
const packageIndexPath = path.join(libPath, 'index.js');
const packageFlexboxIndexPath = path.join(libPath, 'flexbox.js');
const globalPackageIndexPath = path.join(globalPath, 'index.js');
const globalPackageFlexboxIndexPath = path.join(globalPath, 'flexbox.js');
fs.ensureDirSync(path.dirname(globalPath));
fs.copySync(packageIndexPath, globalPackageIndexPath);
fs.copySync(packageFlexboxIndexPath, globalPackageFlexboxIndexPath);
const filter = through2.obj(function componentDirectoryFilter(item, enc, next) {
if (item.stats.isFile()) {
const fileName = path.basename(item.path);
if (fileName === 'index.js') {
const directory = path.dirname(item.path);
const scssPath = path.join(directory, '_styles.scss');
try {
fs.accessSync(item.path, fs.F_OK);
fs.accessSync(scssPath, fs.F_OK);
this.push(item);
} catch (e) {
// Not component directory
}
}
}
next();
});
fs.walk(libPath)
.pipe(filter)
.on('data', (item) => {
const globalComponentPath =
s(item.path)
.reverse()
.replace(
s(`lib${path.sep}`).reverse().value(),
s(`lib${path.sep}global${path.sep}`).reverse().value()
)
.reverse()
.value();
const content = fs.readFileSync(item.path, 'utf8')
.replace(/require\('\.\/\_styles\.scss'\)/g, '{}')
.replace(/require\('classnames\/bind'\)/, 'require(\'classnames\')')
.replace(/require\('\.\.\/util\//g, 'require(\'../../util/')
fs.ensureDirSync(path.dirname(globalComponentPath));
fs.writeFileSync(globalComponentPath, content, 'utf8');
});