-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·215 lines (184 loc) · 4.28 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env node
const chalk = require('chalk');
const prompt = require('prompt');
const fs = require('fs');
const package = require('./package.json');
/**
* Data schema.
*/
const schema = {
properties: {
title: {
message: 'Choose a title for your section',
require: true,
},
description: {
message: 'Define a description for the section',
required: true,
},
},
};
/**
* Paths.
*/
const paths = {
liquid: 'src/sections',
scripts: 'src/scripts/sections',
styles: 'src/styles/sections',
};
/**
* Introduction.
*/
console.log(`\n${chalk.green('sectionise v')}${chalk.green(package.version)}\n`);
if (process.argv.length > 2) {
/**
* Arguments have been provided.
*/
const result = {
title: process.argv[2],
description: process.argv[3],
};
createLiquid(result);
createScript(result);
createStyles(result);
return;
}
/**
* Request section info.
*/
prompt.start();
prompt.get(schema, function(error, result) {
if (error) {
return error;
}
createLiquid(result);
createScript(result);
createStyles(result);
console.log(`\n${chalk.yellow('★')} Section created successfully.\n`);
});
/**
* Create the liquid file.
*/
async function createLiquid(data) {
if (!fs.existsSync(paths.liquid)) {
fs.mkdirSync(paths.liquid, {recursive: true});
}
try {
fs.appendFileSync(`${paths.liquid}/${handleize(data.title)}.liquid`, getSectionTemplate(data));
console.log(`\n${chalk.green('✓')}${chalk.white(' Liquid file created.')}`);
} catch (error) {
return error;
}
}
/**
* Create the script file.
*/
function createScript(data) {
if (!fs.existsSync(paths.scripts)) {
fs.mkdirSync(paths.scripts, {recursive: true});
}
try {
fs.appendFileSync(`${paths.scripts}/${handleize(data.title)}.js`, getScriptTemplate(data));
console.log(`\n${chalk.green('✓')}${chalk.white(' Script file created.')}`);
} catch (error) {
return error;
}
}
/**
* Create the styles file.
*/
function createStyles(data) {
if (!fs.existsSync(paths.styles)) {
fs.mkdirSync(paths.styles, {recursive: true});
}
try {
fs.appendFileSync(`${paths.styles}/${handleize(data.title)}.scss`, getStyleTemplate(data));
console.log(`\n${chalk.green('✓')}${chalk.white(' Style file created.')}`);
} catch (error) {
return error;
}
}
/**
* Returns the section mark up.
*/
function getSectionTemplate(data) {
return `{% comment %}
----------------------------------------------------------------------------
Section: ${data.title}
- ${data.description}
----------------------------------------------------------------------------
{% endcomment %}
<section
class="${handleize(data.title)}"
data-section-type="${handleize(data.title)}"
data-section-id="{{ section.id }}"
>
</section>
`;
}
/**
* Returns the script template.
*/
function getScriptTemplate(data) {
return `/**
* Section: ${data.title}
* ------------------------------------------------------------------------------
* ${data.description}
*
* @namespace ${camelizeHandle(handleize(data.title))}
*/
import {register} from '@shopify/theme-sections';
/**
* Register the \`${handleize(data.title)}\` section type.
*/
register('${handleize(data.title)}', {
/**
* Callback function when section is loaded via 'sections.load()' or by the Theme Editor 'shopify:section:load' event.
*/
onLoad() {
},
});
`;
}
/**
* Returns the style template.
*/
function getStyleTemplate(data) {
return `/**
* Section: ${data.title}
* -----------------------------------------------------------------------------
* ${data.description}
*
*/
.${handleize(data.title)} {
}
`;
}
/**
* Handleizes a string.
* @param {string} string - The string to handleize.
*/
function handleize(string) {
return string.toString().toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '');
}
/**
* Camelizes a handle.
* @param {string} handle - The handle to camelize.
*/
function camelizeHandle(handle) {
let string = ``;
const parts = handle.split('-');
parts.forEach(function(part, index) {
if (index === 0) {
string += part.toLowerCase();
return;
}
string += part.charAt(0).toUpperCase() + part.slice(1)
});
return string;
}