-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathmake_new_command.js
More file actions
107 lines (94 loc) · 3.79 KB
/
Copy pathmake_new_command.js
File metadata and controls
107 lines (94 loc) · 3.79 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { argv } = yargs(hideBin(process.argv));
const fs = require('fs');
const { packageName, commandNameSnakeCase, remove } = argv;
if (!packageName) {
console.error('No package name!');
} else if (!commandNameSnakeCase) {
console.error('No command name!');
}
const defaultPackagesPath = `${__dirname}/../../packages/default-packages`;
// check if "unigraph.${packageName}" exists in "../../packages/default-packages/"
const packagePath = `${defaultPackagesPath}/unigraph.${packageName}`;
if (!fs.existsSync(packagePath)) {
console.error(`${packagePath} does not exist!`);
}
const snakeToKebabCase = (str) => str.replace('_', '-');
const snakeToCamelCase = (str) =>
str
.split('_')
.map((word, idx) => (idx > 0 ? word.charAt(0).toUpperCase() + word.slice(1) : word))
.join('');
const snakeToSentence = (str) =>
str
.split('_')
.map((word, idx) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
const createCommandFile = (snakeCaseCommandName) => {
// Create ${pakagePath}/entities/${snakeCaseCommandName}_command.json
const commandPath = `${packagePath}/entities/${snakeCaseCommandName}_command.json`;
const commandNameSpaceSeparated = snakeToSentence(snakeCaseCommandName);
if (fs.existsSync(commandPath)) {
console.error(`${commandPath} already exists!`);
} else {
// create entities dir if it does not exist
const entitiesDir = `${packagePath}/entities`;
if (!fs.existsSync(entitiesDir)) {
fs.mkdirSync(entitiesDir);
}
fs.writeFileSync(
commandPath,
JSON.stringify(
{
type: { 'unigraph.id': '$/schema/command' },
name: `${commandNameSpaceSeparated} (Command)`,
},
null,
2,
),
);
console.log(`${commandPath} created!`);
}
};
const deleteCommandFile = (snakeCaseCommandName) => {
// Delete ${pakagePath}/entities/${snakeCaseCommandName}_command.json
const commandPath = `${packagePath}/entities/${snakeCaseCommandName}_command.json`;
if (fs.existsSync(commandPath)) {
fs.unlinkSync(commandPath);
}
console.log(`${commandPath} deleted!`);
};
const updatePackageJsonWithEntity = (snakeCaseCommandName) => {
// add {id, src} to packageJson.unigraph.entities
const packageJsonPath = `${packagePath}/package.json`;
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
const entities = packageJson.unigraph.entities || [];
const newEntity = {
id: `${snakeCaseCommandName}_command`,
src: `entities/${snakeCaseCommandName}_command.json`,
};
entities.push(newEntity);
packageJson.unigraph.entities = entities;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`${packageJsonPath} updated!`);
};
const removeEntityFromPackageJson = (snakeCaseCommandName) => {
// remove {id, src} from packageJson.unigraph.entities
const packageJsonPath = `${packagePath}/package.json`;
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
const entities = packageJson.unigraph.entities || [];
const newEntities = entities.filter((entity) => entity.id !== `${snakeCaseCommandName}_command`);
packageJson.unigraph.entities = newEntities;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`${packageJsonPath} updated!`);
};
// RUNNING
if (!remove) {
createCommandFile(commandNameSnakeCase);
updatePackageJsonWithEntity(commandNameSnakeCase);
} else {
deleteCommandFile(commandNameSnakeCase);
removeEntityFromPackageJson(commandNameSnakeCase);
}