This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
79 lines (67 loc) · 1.98 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs');
const path = require('path');
const pkg = require('./package.json');
const getParser = require('./lib/parser');
const getMappers = require('./lib/mappers');
const { prettyJSON, writeFile, downloadFile, uniqueArrayBy } = require('./lib/utils');
const mapToStyleDictionaryTokens = require('./lib/styleDictionary');
const ASSETS_DIR = 'assets';
module.exports = async (args, flags) => {
if (flags.version) return pkg.version;
if (args.length <= 0) throw new Error('No file input passed after npm start');
const { parser } = await getParser(args, flags);
const {
textStyles,
colors,
gradients,
shadows,
borders,
blurs,
fonts,
version,
response,
fileType,
assets,
grid,
} = await parser.getTokens();
const mappers = getMappers(fileType);
const mapping = {
textStyles: uniqueArrayBy(
textStyles.map((txt) => mappers.textStyles(txt, flags.ignoreTextStylePaths)),
'id',
),
colors: colors.map(mappers.colors),
gradients: gradients.map(mappers.gradients),
shadows: shadows.map(mappers.shadows),
borders: borders.map(mappers.borders),
blurs: blurs.map(mappers.blurs),
grid,
fonts,
fileType,
...mappers.version(version),
};
if (!fs.existsSync(flags.outputDir)) {
fs.mkdirSync(flags.outputDir);
}
if (flags.useStyleDictionaryOutput) {
await writeFile(
`${flags.outputDir}/hubble-style-dictionary-tokens.json`,
prettyJSON(mapToStyleDictionaryTokens(mapping)),
);
} else {
await writeFile(`${flags.outputDir}/hubble-data.json`, prettyJSON(mapping));
}
if (flags.exportAssets && assets) {
const fullAssetsDir = path.join(flags.outputDir, ASSETS_DIR);
if (!fs.existsSync(fullAssetsDir)) {
fs.mkdirSync(fullAssetsDir);
}
assets.forEach(asset => {
downloadFile(fullAssetsDir, asset);
});
}
if (flags.dump) {
await writeFile(`${flags.outputDir}/raw_output.json`, prettyJSON(response));
}
return response;
};