2018-09-19 22:47:15 +00:00
|
|
|
import * as plugins from './smartsass.plugins';
|
2017-03-05 17:29:46 +00:00
|
|
|
|
|
|
|
// interfaces
|
2018-09-19 22:47:15 +00:00
|
|
|
import { Result } from 'node-sass';
|
2017-03-05 17:29:46 +00:00
|
|
|
|
|
|
|
export interface ISmartsassConstructorOptions {
|
2018-09-19 22:47:15 +00:00
|
|
|
data?: string;
|
|
|
|
entryFilePath?: string;
|
|
|
|
includePaths?: string[];
|
2017-03-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Smartsass {
|
2018-09-19 22:47:15 +00:00
|
|
|
includePaths = [];
|
|
|
|
entryFilePath: string;
|
2018-09-20 21:06:26 +00:00
|
|
|
data: string;
|
2017-03-05 17:29:46 +00:00
|
|
|
constructor(optionsArg: ISmartsassConstructorOptions) {
|
2018-09-19 22:47:15 +00:00
|
|
|
this.entryFilePath = optionsArg.entryFilePath;
|
2018-09-20 21:06:26 +00:00
|
|
|
this.data = optionsArg.data;
|
2017-03-11 23:56:31 +00:00
|
|
|
if (optionsArg.includePaths) {
|
2017-03-11 23:55:34 +00:00
|
|
|
for (let includePath of optionsArg.includePaths) {
|
2018-09-19 22:47:15 +00:00
|
|
|
this.includePaths.push(includePath);
|
2017-03-11 23:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 09:59:21 +00:00
|
|
|
/**
|
|
|
|
* add further include paths
|
|
|
|
*/
|
2018-09-19 22:47:15 +00:00
|
|
|
addIncludePaths(includePathsArray: string[]) {
|
2017-03-12 09:59:21 +00:00
|
|
|
for (let includePath of includePathsArray) {
|
2018-09-19 22:47:15 +00:00
|
|
|
this.includePaths.push(includePath);
|
2017-03-12 09:59:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 17:29:46 +00:00
|
|
|
/**
|
|
|
|
* renders the Smartsass classes' entryfile and returns result as string
|
|
|
|
*/
|
|
|
|
render() {
|
2018-09-19 22:47:15 +00:00
|
|
|
let done = plugins.smartpromise.defer<plugins.sass.Result>();
|
|
|
|
plugins.sass.render(
|
|
|
|
{
|
2018-09-20 21:06:26 +00:00
|
|
|
data: this.data,
|
2018-09-19 22:47:15 +00:00
|
|
|
file: this.entryFilePath,
|
|
|
|
includePaths: this.includePaths
|
|
|
|
},
|
|
|
|
function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
done.reject(err);
|
|
|
|
}
|
|
|
|
done.resolve(result);
|
2017-03-05 17:29:46 +00:00
|
|
|
}
|
2018-09-19 22:47:15 +00:00
|
|
|
);
|
|
|
|
return done.promise;
|
2017-03-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* renders and stores
|
|
|
|
*/
|
2018-09-19 22:47:15 +00:00
|
|
|
async renderAndStore(outputFilePath: string) {
|
|
|
|
let result = await this.render();
|
|
|
|
await plugins.smartfile.memory.toFs(result.css.toString(), outputFilePath);
|
2017-03-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
}
|