forked from mikaelvesavuori/figmagic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·68 lines (54 loc) · 2.23 KB
/
index.ts
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
#! node
import * as path from 'path';
import * as dotenv from 'dotenv';
import { makeConfiguration } from './bin/entities/Config/index';
import { FigmaData } from './bin/contracts/FigmaData';
import { Config } from './bin/contracts/Config';
import { FigmagicController } from './bin/controllers/FigmagicController';
import { write } from './bin/frameworks/filesystem/write';
import { getData } from './bin/frameworks/network/getData';
import { writeBaseJson } from './bin/frameworks/filesystem/writeBaseJson';
import { colors } from './bin/frameworks/system/colors';
import { checkIfExists } from './bin/frameworks/filesystem/checkIfExists';
import { configToInit } from './bin/frameworks/system/configToInit';
import { MsgJobCompleteInit, MsgJobCompleteInitStopped } from './bin/frameworks/messages/messages';
const RC_FILE = '.figmagicrc';
/**
* @description Initialize and setup Figmagic (environment; configuration; data) before handing over to the controller
*/
async function main(): Promise<void> {
try {
// Setup environment and user configuration
dotenv.config();
// User wants to init a configuration...
const [, , ...CLI_ARGS] = process.argv;
if (CLI_ARGS[0]?.toLowerCase() === 'init') initConfig(configToInit);
// User wants to run Figmagic
else {
const USER_CONFIG_PATH = path.join(`${process.cwd()}`, RC_FILE);
const CONFIG: Config = await makeConfiguration(USER_CONFIG_PATH, ...CLI_ARGS);
// Get data
const { recompileLocal, figmagicFolder, figmaData, token, url } = CONFIG;
const DATA: FigmaData = await getData(recompileLocal, figmagicFolder, figmaData, token, url);
// Write new JSON base data, unless user explicitly opts out
if (!recompileLocal) await writeBaseJson(figmagicFolder, figmaData, DATA);
// Run the controller
await FigmagicController(CONFIG, DATA);
}
} catch (error) {
console.error(`${colors.FgRed}${error}`);
}
}
/**
* @description Handle basic configuration initialization
*/
function initConfig(file: any) {
const FILE_EXISTS = checkIfExists(RC_FILE);
if (!FILE_EXISTS) {
write(RC_FILE, JSON.stringify(file, null, ' '));
console.log(MsgJobCompleteInit);
return;
}
console.log(MsgJobCompleteInitStopped);
}
main();