-
-
Notifications
You must be signed in to change notification settings - Fork 15
Fix non-working Ctrl+c on windows #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
var converter = require('./converter'); | ||
|
||
module.exports = function ($logger) { | ||
var watcher = converter.getWatcher(); | ||
if (watcher) { | ||
$logger.info("Stopping nativescript-dev-sass watcher"); | ||
watcher.close(); | ||
} | ||
} | ||
converter.dispose(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
exports.compile = compile; | ||
|
||
var fs = require("fs"); | ||
var path = require('path'); | ||
var spawn = require("child_process").spawn; | ||
var LogProvider = require('./log-provider'); | ||
|
||
var currentSassProcess = null; | ||
|
||
function compile(data) { | ||
if (currentSassProcess) { | ||
return Promise.resolve(); | ||
} | ||
|
||
return new Promise((res, rej) => { | ||
var projectDir = data.projectDir, | ||
appDir = data.appDir; | ||
|
||
var logger = new LogProvider(data.logger); | ||
|
||
var sassPath = require.resolve('node-sass/bin/node-sass'); | ||
if (fs.existsSync(sassPath)) { | ||
logger.info("Found peer node-sass"); | ||
} else { | ||
throw new Error('node-sass installation local to project was not found. Install by executing `npm install node-sass`.'); | ||
} | ||
|
||
var isResolved = false; | ||
var resolve = () => { | ||
if (isResolved) { | ||
return; | ||
} | ||
|
||
isResolved = true; | ||
res(currentSassProcess); | ||
} | ||
var reject = err => { | ||
if (isResolved) { | ||
return; | ||
} | ||
|
||
isResolved = true; | ||
|
||
err.errorAsWarning = true; | ||
err.stopExecution = false; | ||
|
||
rej(err); | ||
} | ||
|
||
// Node SASS Command Line Args (https://github.com/sass/node-sass#command-line-interface) | ||
// --ouput : Output directory | ||
// --output-style : CSS output style (nested | expanded | compact | compresed) | ||
// -q : Supress log output except on error | ||
// --follow : Follow symlinked directories | ||
// -r : Recursively watch directories or files | ||
// --watch : Watch a directory or file | ||
var nodeArgs = [sassPath, appDir, '--output', appDir, '--output-style', 'compressed', '-q', '--follow', '--importer', path.join(__dirname, "importer.js")]; | ||
logger.trace(process.execPath, nodeArgs.join(' ')); | ||
|
||
var env = Object.create(process.env); | ||
env.PROJECT_DIR = projectDir; | ||
env.APP_DIR = appDir; | ||
|
||
currentSassProcess = spawn(process.execPath, nodeArgs, { env: env }); | ||
|
||
currentSassProcess.stdout.on('data', data => { | ||
var stringData = data.toString(); | ||
logger.info(stringData); | ||
}); | ||
|
||
currentSassProcess.stderr.on('data', error => { | ||
var message = ''; | ||
var stringData = error.toString(); | ||
|
||
try { | ||
var parsed = JSON.parse(stringData); | ||
message = parsed.formatted || parsed.message || stringData; | ||
} catch (e) { | ||
message = error.toString(); | ||
} | ||
|
||
logger.info(message); | ||
}); | ||
|
||
currentSassProcess.on('error', error => { | ||
logger.info(err.message); | ||
reject(error); | ||
}); | ||
|
||
currentSassProcess.on('exit', (code, signal) => { | ||
if (code === 0) { | ||
resolve(); | ||
} else { | ||
reject(new Error(`SASS compiler failed with exit code ${code}`)); | ||
} | ||
|
||
currentSassProcess = null; | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,51 @@ | ||
exports.convert = convert; | ||
exports.getWatcher = getWatcher; | ||
exports.dispose = dispose; | ||
|
||
var sassCompiler = require('./compiler'); | ||
var spawn = require('child_process').spawn; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var choki = require('chokidar'); | ||
var watcher = null; | ||
var watchPromisesChain = Promise.resolve(); | ||
|
||
var watcherProcess = null; | ||
|
||
function convert(logger, projectDir, appDir, options) { | ||
options = options || {}; | ||
var sassPath = getSassPath(logger); | ||
var data = { | ||
sassPath, | ||
projectDir, | ||
appDir, | ||
logger, | ||
options | ||
logger | ||
}; | ||
|
||
if (options.watch) { | ||
createWatcher(data); | ||
return; | ||
} | ||
|
||
return spawnNodeSass(data); | ||
} | ||
|
||
function getWatcher() { | ||
return watcher; | ||
return sassCompiler.compile(data); | ||
} | ||
|
||
function createWatcher(data) { | ||
var appDir = data.appDir; | ||
var watcherOptions = { | ||
ignoreInitial: true, | ||
cwd: appDir, | ||
awaitWriteFinish: { | ||
pollInterval: 100, | ||
stabilityThreshold: 300 | ||
}, | ||
ignored: ['**/.*', '.*'] // hidden files | ||
}; | ||
if (watcherProcess) { | ||
return; | ||
} | ||
|
||
watcherProcess = spawn(process.execPath, [ path.join(__dirname, "./watcher.js"), JSON.stringify({appDir: data.appDir, projectDir: data.projectDir })], { stdio: ["ignore", "ignore", "ignore", "ipc"] }); | ||
|
||
watcher = choki.watch(['**/*.scss', '**/*.sass'], watcherOptions) | ||
.on('all', (event, filePath) => { | ||
watchPromisesChain = watchPromisesChain | ||
.then(() => spawnNodeSass(data)) | ||
.catch(err => { | ||
if (!err.stopExecution && err.errorAsWarning) { | ||
data.logger.warn(err.message); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
}); | ||
watcherProcess.on('error', error => { | ||
throw new Error(error); | ||
}); | ||
|
||
watcherProcess.on('message', message => { | ||
if (message && message.logLevel) { | ||
data.logger[message.logLevel](message.message); | ||
} | ||
}); | ||
} | ||
|
||
function getSassPath(logger) { | ||
var sassPath = require.resolve('node-sass/bin/node-sass'); | ||
if (fs.existsSync(sassPath)) { | ||
logger.info('Found peer node-sass'); | ||
} else { | ||
throw new Error('node-sass installation local to project was not found. Install by executing `npm install node-sass`.'); | ||
function dispose() { | ||
if (watcherProcess) { | ||
watcherProcess.disconnect(); | ||
watcherProcess = null; | ||
} | ||
|
||
return sassPath; | ||
} | ||
|
||
function spawnNodeSass(data) { | ||
return new Promise(function (resolve, reject) { | ||
var sassPath = data.sassPath, | ||
projectDir = data.projectDir, | ||
appDir = data.appDir, | ||
logger = data.logger, | ||
options = data.options; | ||
|
||
var importerPath = path.join(__dirname, "importer.js"); | ||
|
||
// Node SASS Command Line Args (https://github.com/sass/node-sass#command-line-interface) | ||
// --ouput : Output directory | ||
// --output-style : CSS output style (nested | expanded | compact | compresed) | ||
// -q : Supress log output except on error | ||
// --follow : Follow symlinked directories | ||
// -r : Recursively watch directories or files | ||
// --watch : Watch a directory or file | ||
var nodeArgs = [sassPath, appDir, '--output', appDir, '--output-style', 'compressed', '-q', '--follow', '--importer', importerPath]; | ||
logger.trace(process.execPath, nodeArgs.join(' ')); | ||
|
||
var env = Object.create( process.env ); | ||
env.PROJECT_DIR = projectDir; | ||
env.APP_DIR = appDir; | ||
|
||
var currentSassProcess = spawn(process.execPath, nodeArgs, { env: env }); | ||
|
||
var isResolved = false; | ||
|
||
currentSassProcess.stdout.on('data', function (data) { | ||
var stringData = data.toString(); | ||
logger.info(stringData); | ||
}); | ||
|
||
currentSassProcess.stderr.on('data', function (err) { | ||
var message = ''; | ||
var stringData = err.toString(); | ||
|
||
try { | ||
var parsed = JSON.parse(stringData); | ||
message = parsed.formatted || parsed.message || stringData; | ||
} catch (e) { | ||
message = err.toString(); | ||
} | ||
|
||
logger.info(message); | ||
}); | ||
|
||
currentSassProcess.on('error', function (err) { | ||
logger.info(err.message); | ||
if (!isResolved) { | ||
isResolved = true; | ||
err.errorAsWarning = true; | ||
err.stopExecution = false; | ||
reject(err); | ||
} | ||
}); | ||
|
||
// TODO: Consider using close event instead of exit | ||
currentSassProcess.on('exit', function (code, signal) { | ||
currentSassProcess = null; | ||
if (!isResolved) { | ||
isResolved = true; | ||
if (code === 0) { | ||
resolve(); | ||
} else { | ||
var error = new Error('SASS compiler failed with exit code ' + code); | ||
error.errorAsWarning = true; | ||
error.stopExecution = false; | ||
reject(error); | ||
} | ||
} | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const INFO_LOG_LEVEL = "info"; | ||
const TRACE_LOG_LEVEL = "trace"; | ||
const WARN_LOG_LEVEL = "warn"; | ||
|
||
module.exports = function(logger) { | ||
this.info = (message) => { | ||
this.logData({logLevel: INFO_LOG_LEVEL, message}); | ||
} | ||
|
||
this.trace = (message) => { | ||
this.logData({logLevel: TRACE_LOG_LEVEL, message}); | ||
} | ||
|
||
this.warn = (message) => { | ||
this.logData({logLevel: WARN_LOG_LEVEL, message}); | ||
} | ||
|
||
this.logData = (data) => { | ||
if (logger) { | ||
return logger[data.logLevel](data.message); | ||
} | ||
|
||
process.send(JSON.stringify(data)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can just send the data, no need to stringify it |
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var choki = require('chokidar'); | ||
var path = require('path'); | ||
var compiler = require('./compiler'); | ||
var LogProvider = require('./log-provider'); | ||
|
||
var args = JSON.parse(process.argv[2]); | ||
var appDir = args.appDir; | ||
var projectDir = args.projectDir; | ||
var watchPromisesChain = Promise.resolve(); | ||
|
||
var watcherOptions = { | ||
ignoreInitial: true, | ||
cwd: appDir, | ||
awaitWriteFinish: { | ||
pollInterval: 100, | ||
stabilityThreshold: 300 | ||
}, | ||
ignored: ['**/.*', '.*', path.join(appDir, "App_Resources")] // hidden files and App_Resources folder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we need to use the projectData.appDirectoryPath here |
||
}; | ||
|
||
watcher = choki.watch('**/*.s[ac]ss', watcherOptions) | ||
.on('all', (event, filePath) => { | ||
watchPromisesChain = watchPromisesChain | ||
.then(() => compiler.compile({appDir, projectDir})) | ||
.catch(err => { | ||
if (!err.stopExecution && err.errorAsWarning) { | ||
var logger = new LogProvider(null); | ||
logger.warn(err.message); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this should be
reject(new Error(...))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and set
isResolved
to true