Skip to content

Commit a5187fb

Browse files
Semih KEŞKEKSemih KEŞKEK
authored andcommitted
Complete SASS Compiler
1 parent 0b7057b commit a5187fb

File tree

6 files changed

+88
-11
lines changed

6 files changed

+88
-11
lines changed

App/Archiver.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path="../typings/index.d.ts" />
2+
import * as File from 'fs';
3+
import * as Zipper from 'zip-folder';
4+
5+
export module Archiver{
6+
export function createArchive(sitePath) {
7+
var historyPath = sitePath.replace(/^Sites/, 'History');
8+
9+
try {
10+
if(!File.lstatSync(historyPath).isDirectory()) throw "Directory Not Found";
11+
} catch (error) {
12+
File.mkdirSync(historyPath);
13+
}
14+
15+
let date = new Date();
16+
let dateArr = [date.getFullYear(), date.getMonth(), date.getDate(), '-', date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()];
17+
dateArr.forEach(function (e, i) {
18+
if(e < 10)
19+
dateArr[i] = '0' + e;
20+
if(dateArr[i] < 100) dateArr[i] = '0' + dateArr[i];
21+
});
22+
23+
let fileName = dateArr.join('.') + '.zip';
24+
Zipper(sitePath, historyPath + fileName, function(err) {});
25+
26+
}
27+
}

App/Configuration.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ var Directory = {
22
SitePath: './Sites'
33
}
44

5+
var SassSettings = {
6+
StartFile: 'style.scss',
7+
OutputFile: 'style.css',
8+
OutputStyle: 'compact',
9+
SourceMap: true
10+
}
11+
512

613

7-
export {Directory};
14+
export {
15+
Directory,
16+
SassSettings
17+
};

App/Exceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var WatcherExceptions = {
22
InvalidPath: "Error Code: WE001IP \n"
3-
+ "Path must be a string."
3+
+ "Path must be a string.",
44
}
55

66
export {

App/Listener.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as Chokidar from 'chokidar';
44
import * as Path from 'path';
55
import {WatcherExceptions} from './Exceptions';
66

7+
import {SASS} from './SASSCompiler'
8+
79

810
export class WatchManager{
911
mainPath: string;
@@ -25,10 +27,7 @@ export class WatchManager{
2527
isSubDirctory: (paths.length > 2 ? true : false),
2628
Path: path,
2729
Watcher: new Watch(path),
28-
subDirectoryName: ''
2930
}
30-
if(paths.length > 2 && false)
31-
this.WatchList[this.WatchList.length - 1].subDirectoryName = paths[2];
3231
});
3332
}
3433
}
@@ -59,9 +58,7 @@ class Watch{
5958
if(this.Event)
6059
clearTimeout(this.Event);
6160
this.Event = setTimeout(function () {
62-
console.log(event);
63-
console.log(path);
64-
console.log(details);
61+
SASS.render(details.watchedPath);
6562
},50);
6663

6764
});
@@ -80,7 +77,6 @@ interface ISiteWatcher{
8077
Path: string;
8178
Watcher: Watch;
8279
isSubDirctory: boolean;
83-
subDirectoryName: string;
8480
}
8581
interface ISubDirectoryWatcher{
8682
Path: string;

App/SASSCompiler.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// <reference path="../typings/index.d.ts" />
2+
import * as NodeSASS from 'node-sass';
3+
import * as File from 'fs';
4+
import {SassSettings} from './Configuration';
5+
import {Archiver} from './Archiver';
6+
7+
export module SASS{
8+
export function render(path) {
9+
if(!path.match(/scss/)) return {
10+
Status: 'NOT RENDERED'
11+
}
12+
path = path.replace(/\\/g, '/');
13+
var directoryHierarchy = path.split('/');
14+
15+
var sitePath = directoryHierarchy[0] + '/' + directoryHierarchy[1] + '/';
16+
17+
let sassResult;
18+
try {
19+
sassResult = NodeSASS.renderSync({
20+
file: sitePath + SassSettings.StartFile,
21+
outputStyle: SassSettings.OutputStyle,
22+
outFile: sitePath + SassSettings.OutputFile,
23+
sourceMap: SassSettings.SourceMap
24+
});
25+
26+
File.writeFileSync(sitePath + SassSettings.OutputFile, sassResult.css);
27+
File.writeFileSync(sitePath + SassSettings.OutputFile + '.map', sassResult.map);
28+
console.log(sassResult.stats);
29+
30+
// Archive Here
31+
Archiver.createArchive(sitePath);
32+
} catch (error) {
33+
console.log(error.formatted || error);
34+
let errorCSS = "body:after{" +
35+
"content: '" + (error.formatted || error).replace(/\n/g,'\\A').replace(/'/g, '') + "';" +
36+
"position:fixed;top: 0;left: 0;width: 100%; height: 100%; background: white; color: black; font-family: monospace;font-size: 30px; white-space:pre" +
37+
"}";
38+
File.writeFileSync(sitePath + SassSettings.OutputFile, errorCSS);
39+
}
40+
41+
}
42+
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "sass-compiler",
2+
"name": "sass-multi.compiler",
33
"version": "1.0.0",
44
"description": "Live SASS Compiler",
55
"main": "app.js",
@@ -28,7 +28,9 @@
2828
},
2929
"homepage": "https://github.com/sqlProvider/SASS-Compiler#readme",
3030
"dependencies": {
31+
"archive-directory": "^2.0.0",
3132
"chokidar": "^1.6.0",
32-
"node-sass": "^3.8.0"
33+
"node-sass": "^3.8.0",
34+
"zip-folder": "^1.0.0"
3335
}
3436
}

0 commit comments

Comments
 (0)