Handle shebang in ScriptFileStorage load/save
Before this change, ScriptFileStorage.load was returning source code including shebang (#!/usr/bin/node). This prevented the front-end from matching the file on disk with the script loaded in Node/V8. As a result, `node-debug _mocha` did not show the breakpoint in the _mocha file. ScriptFileStorage.save is preserving shebang when saving the updated file now, so that live edit works as expected.
This commit is contained in:
Родитель
ffbfbe9e92
Коммит
5f95f5b201
|
@ -37,9 +37,18 @@ $class.save = function(path, content, callback) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var newSource = match[1];
|
var newSource = match[1];
|
||||||
fs.writeFile(path, newSource, function(err) {
|
async.waterfall([
|
||||||
callback(err);
|
fs.readFile.bind(fs, path, 'utf-8'),
|
||||||
});
|
|
||||||
|
function(oldContent, cb) {
|
||||||
|
var match = /^(\#\!.*)/.exec(oldContent);
|
||||||
|
if (match)
|
||||||
|
newSource = match[1] + newSource;
|
||||||
|
|
||||||
|
fs.writeFile(path, newSource, cb);
|
||||||
|
}
|
||||||
|
],
|
||||||
|
callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -52,6 +61,10 @@ $class.load = function(path, callback) {
|
||||||
{ encoding: 'utf-8' },
|
{ encoding: 'utf-8' },
|
||||||
function(err, content) {
|
function(err, content) {
|
||||||
if (err) return callback(err);
|
if (err) return callback(err);
|
||||||
|
|
||||||
|
// remove shebang
|
||||||
|
content = content.replace(/^\#\!.*/, '');
|
||||||
|
|
||||||
var source = MODULE_HEADER + content + MODULE_TRAILER;
|
var source = MODULE_HEADER + content + MODULE_TRAILER;
|
||||||
return callback(null, source);
|
return callback(null, source);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,21 @@ describe('ScriptFileStorage', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('preserves shebang when saving new content', function(done) {
|
||||||
|
runLiveEdit(
|
||||||
|
function(content) { return '#!/usr/bin/node\n' + content; },
|
||||||
|
function(debuggerClient, originalScript, runtimeScript) {
|
||||||
|
var storage = new ScriptFileStorage();
|
||||||
|
storage.save(TEMP_FILE, edited(runtimeScript), function(err) {
|
||||||
|
if (err) throw err;
|
||||||
|
var newScript = fs.readFileSync(TEMP_FILE, { encoding: 'utf-8' });
|
||||||
|
expect(newScript).to.equal(edited(originalScript));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('loads content with node.js module wrapper', function(done) {
|
it('loads content with node.js module wrapper', function(done) {
|
||||||
fs.writeFileSync(TEMP_FILE, '/* content */');
|
fs.writeFileSync(TEMP_FILE, '/* content */');
|
||||||
storage.load(TEMP_FILE, function(err, content) {
|
storage.load(TEMP_FILE, function(err, content) {
|
||||||
|
@ -40,6 +55,16 @@ describe('ScriptFileStorage', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('loads content without shebang', function(done) {
|
||||||
|
fs.writeFileSync(TEMP_FILE, '#!/usr/bin/env/node\n/* content */');
|
||||||
|
storage.load(TEMP_FILE, function(err, content) {
|
||||||
|
if (err) throw err;
|
||||||
|
expect(content).to.not.contain('#!');
|
||||||
|
expect(content).to.contain('/* content */');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('finds application root for subdir/app.js by checking package.json file in parent',
|
it('finds application root for subdir/app.js by checking package.json file in parent',
|
||||||
function(done) {
|
function(done) {
|
||||||
givenTempFiles('subdir/', 'subdir/app.js', 'package.json');
|
givenTempFiles('subdir/', 'subdir/app.js', 'package.json');
|
||||||
|
@ -217,8 +242,13 @@ describe('ScriptFileStorage', function() {
|
||||||
return source.replace(';', '; /* edited */');
|
return source.replace(';', '; /* edited */');
|
||||||
}
|
}
|
||||||
|
|
||||||
function runLiveEdit(callback) {
|
function runLiveEdit(transformSource, callback) {
|
||||||
var originalScript = createTempFileAsCopyOf('LiveEdit.js');
|
if (!callback) {
|
||||||
|
callback = transformSource;
|
||||||
|
transformSource = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var originalScript = createTempFileAsCopyOf('LiveEdit.js', transformSource);
|
||||||
launcher.startDebugger(TEMP_FILE, function(childProcess, debuggerClient) {
|
launcher.startDebugger(TEMP_FILE, function(childProcess, debuggerClient) {
|
||||||
getScriptSourceByName(debuggerClient, TEMP_FILE, function(source) {
|
getScriptSourceByName(debuggerClient, TEMP_FILE, function(source) {
|
||||||
callback(debuggerClient, originalScript, source);
|
callback(debuggerClient, originalScript, source);
|
||||||
|
@ -226,9 +256,10 @@ describe('ScriptFileStorage', function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function createTempFileAsCopyOf(fixture) {
|
function createTempFileAsCopyOf(fixture, transform) {
|
||||||
var sourcePath = path.join(__dirname, 'fixtures', fixture);
|
var sourcePath = path.join(__dirname, 'fixtures', fixture);
|
||||||
var content = fs.readFileSync(sourcePath, { encoding: 'utf-8' });
|
var content = fs.readFileSync(sourcePath, { encoding: 'utf-8' });
|
||||||
|
if (transform) content = transform(content);
|
||||||
fs.writeFileSync(TEMP_FILE, content);
|
fs.writeFileSync(TEMP_FILE, content);
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче