-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
170 lines (128 loc) · 5.08 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
const fs = require('fs');
const sass = require('sass');
const path = require('path');
const version = require('./package.json').version;
const get_gha_input = (name) => { return process.env[`INPUT_${name.toUpperCase()}`]; };
const source = get_gha_input('source').split('\n');
const destination = get_gha_input('destination').split('\n');
if (source === undefined || destination === undefined) {
const error_message = [
'Required environment variable(s) are undefined!',
` source -> ${source}`,
` destination -> ${destination}`,
'Please assign missing environment variables via',
' INPUT_SOURCE="path/to/main.scss"\\',
' INPUT_DESTINATION="assets/css/main.css"\\',
' node index.js'
];
throw new ReferenceError(error_message.join('\n'));
}
const render_options = {
outputStyle: 'compressed',
indentWidth: 2 // Cannot be `NaN` https://github.com/gha-utilities/sass-build/issues/11
};
/**
* Optional inputs with predictable types
*/
const boolean_gha_input_names = [
'sourceComments', // Boolean, default `false` if `true` emits comments where CSS was compiled from
'omitSourceMapUrl', // Boolean, when `true` compiled CSS is not linked to source map
'sourceMapContents', // Boolean, when `true` embeds contents of Sass files that contributed to compiled CSS
'sourceMapEmbed', // Boolean, when `true` embeds source map within compiled CSS
];
const integer_gha_input_names = [
'precision', // Integer and float precision when compiling CSS, eg. `20`
'indentWidth', // Integer, of tabs/spaces used to indent, eg. `1`
];
const string_gha_input_names = [
'outputStyle', // String, either 'expanded', 'compressed', 'nested', 'compact'
'indentType', // String, either 'space' or 'tab', default 'space'
'linefeed', // String, either 'lf', 'lfcr', 'cr', or 'crlf', default 'lf'
'outFile', // String, does not write but is useful in combination with 'sourceMap'
'sourceMapRoot', // String, prepended to all links from from source map to Sass files
];
boolean_gha_input_names.forEach((name) => {
const env_value = get_gha_input(name);
if (env_value === 'true' || env_value === 'false') {
render_options[name] = (env_value === 'true');
}
});
integer_gha_input_names.forEach((name) => {
const env_value = get_gha_input(name);
if (Number.parseInt(env_value) !== NaN) {
render_options[name] = Number.parseInt(env_value);
}
});
string_gha_input_names.forEach((name) => {
const env_value = get_gha_input(name);
if (env_value !== undefined && env_value !== '') {
render_options[name] = get_gha_input(name);
}
});
/**
* Inputs that require a bit more care
*/
// 'includePaths' // Array, directories to look under for imports and used modules, splits on ':'
const includePaths = get_gha_input('includePaths');
if (includePaths !== undefined && includePaths !== '') {
render_options['includePaths'] = includePaths.split(':');
}
// 'sourceMap' // May be boolean or string, see https://sass-lang.com/documentation/js-api#sourcemap
const sourceMap = get_gha_input('sourceMap');
if (sourceMap === 'true' || sourceMap === 'false') {
render_options['sourceMap'] = (sourceMap === 'true');
} else if (sourceMap !== undefined && sourceMap !== '') {
render_options['sourceMap'] = sourceMap;
}
/**
* Compiles CSS and write it to file path
* @param {string} source_file - Path to SCSS or SASS file to parse
* @param {string} destination_file - Path to CSS file to write
*/
function build_CSS(source_file, destination_file) {
render_options['file'] = source_file;
if (get_gha_input('debug')) {
console.log('--- render_options ---');
console.table(render_options);
}
const sass_result = sass.renderSync(render_options);
/**
* Write CSS to file path
*/
fs.stat(destination_file, (err, stat) => {
if (err && err.code === 'ENOENT') {
const warnning_message = [
`Warning: ${err.message}`,
`Attempting to write to file path -> ${destination_file}`,
];
console.warn(warnning_message.join('\n'));
} else if (stat.isDirectory()) {
destination_file = `${destination_file}/${path.basename(source_file)}`;
const warnning_message = [
`Warning: destination path was converted to -> "${destination_file}"`,
'To avoid this warning please assign `destination` to a file path, eg...',
' - name: Compile CSS from SCSS files',
` uses: gha-utilities/sass-build@v${version}`,
' with:',
` source: ${source_file}`,
` destination: ${destination_file}`,
];
console.warn(warnning_message.join('\n'));
}
fs.writeFile(destination_file, sass_result.css, (err) => {
if (err) {
throw err;
} else {
console.log(`Wrote file -> ${destination_file}`);
}
});
});
}
/**
* @note - Last element of sources & destinations is an empty string
*/
for (let i = 0; i < source.length; i++) {
if (!['', undefined].includes(source[i]) && !['', undefined].includes(destination[i])) {
build_CSS(source[i], destination[i]);
}
}