UNPKG

3.87 kBJavaScriptView Raw
1/**
2
3 locals-editor.js
4
5**/
6
7
8//
9// Dependencies
10//
11
12var chalk = require("chalk"),
13 utils = require("./cli-utils");
14
15
16
17//
18// Exports
19//
20
21var Vars = module.exports = {
22
23 addEnv: function(key, val) {
24 Vars.add("env", key.toUpperCase(), val);
25 },
26
27 addFavicon: function(val) {
28 Vars.add(null, "favicon", val);
29 },
30
31 addLocal: function(key, val) {
32
33 // Unix won't process arguments with $ in them. Provide a warning if this may have happened
34 if ("" === val) {
35 var msg = "If you're trying to add a local variable linked to environment, make sure to escape it: ";
36 console.log("\n" + chalk.magenta(" Warning!"), msg);
37 console.log(" e.g. " + chalk.yellow("shipp local:add siteName \"\$SITE_NAME\""));
38 }
39
40 Vars.add("locals", key, val);
41 },
42
43 listEnvs: function() {
44 Vars.list("env", "Environment Variables");
45 },
46
47 listFavicon: function() {
48 Vars.list("favicon", "Favicon");
49 },
50
51 listLocals: function() {
52 Vars.list("locals", "Locals");
53 },
54
55 removeEnv: function(key, val) {
56 Vars.remove("env", key.toUpperCase());
57 },
58
59 removeFavicon: function() {
60 Vars.remove(null, "favicon");
61 },
62
63 removeLocal: function(key, val) {
64 Vars.remove("locals", key);
65 },
66
67
68 /**
69
70 Outputs a list of local variables.
71
72 @param key The key to lookup
73 @param title The title to output
74
75 **/
76
77 list: function(key, title) {
78
79 var editor = require("./config-editor"),
80 val = editor.get(key) || {},
81 str;
82
83 console.log("");
84 console.log(chalk.cyan(" " + title + ":"));
85 console.log("");
86
87 if (!Object.keys(val).length)
88 console.log(" There are no " + key + " variables in your project");
89 else {
90 if ("string" === typeof val)
91 str = " Your " + chalk.yellow(key) + " is set to " + chalk.yellow(val);
92 else
93 str = JSON.stringify(val, null, 2).split("\n").map(function(line) { return " " + line; }).join("\n");
94 console.log(str);
95 }
96
97 console.log("");
98
99 },
100
101
102 /**
103
104 Adds a local variable and acknolwedges with confirmation.
105
106 @param {String} parent Which variable to change (e.g. locals, env)
107 @param {String} key The key to lookup (supports dot-notation)
108 @param {*} val The value to assign
109
110 **/
111
112 add: function(parent, key, val) {
113
114 var editor = require("./config-editor"),
115 ref = (parent) ? parent + "." + key : key;
116
117 console.log("");
118
119 // Perform type casting
120 if (val === "false")
121 val = false;
122 else if (val === "true")
123 val = true;
124 else if (/^\-?([0-9]+\.?[0-9]*|\.[0-9]*)$/.test(val))
125 val = parseFloat(val);
126
127 editor.set(ref, val);
128 editor.save();
129
130 parent = (parent) ? " " + parent : ""
131 console.log(" " + chalk.cyan("Added:") + parent + " variable " + chalk.yellow(key) + " now contains " + chalk.yellow(val));
132 console.log("");
133
134 },
135
136
137 /**
138
139 Removes a local variable and acknowledges with confirmation.
140
141 @param {String} parent Which variable to change (e.g. locals, env)
142 @param {String} key The key to remove (supports dot-notation)
143
144 **/
145
146 remove: function(parent, key) {
147
148 var editor = require("./config-editor"),
149 ref = (parent) ? parent + "." + key : key;
150 prev = editor.get(ref);
151
152 console.log("");
153
154 editor.unset(ref);
155 editor.save();
156
157 parent = (parent) ? " " + parent : ""
158 if ("undefined" === typeof prev)
159 console.log(" " + chalk.red("No Change:") + " there was no" + parent + " variable called " + chalk.yellow(key));
160 else if ("object" === typeof prev)
161 console.log(" " + chalk.cyan("Removed:") + parent + " variable " + chalk.yellow(key) + " has been unset");
162 else
163 console.log(" " + chalk.cyan("Removed:") + parent + " variable " + chalk.yellow(key) + " has been unset, previously " + chalk.yellow(prev));
164 console.log("");
165
166 }
167
168};