Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from manifoldjs/platforms-update
Browse files Browse the repository at this point in the history
Platforms update
  • Loading branch information
boyofgreen committed Feb 3, 2016
2 parents 8511db7 + 01dfbdc commit 45ee7dd
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 208 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ManifoldJS
manifoldjs-lib

Copyright (c) Microsoft Corporation

Expand Down
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
# manifoldjs-lib
ManifoldJS Core Library
# manifoldjs-lib

## ManifoldJS Core Library

The library contains the core modules required by [ManifoldJS](https://github.com/manifoldjs/ManifoldJS), a tool for creating hosted web applications based on a [W3C Web App manifest](http://www.w3.org/TR/appmanifest/).

## Installation

```
npm install manifoldjs-lib
```
In node.js:

```
var lib = require('manifoldjs-lib')
```

## Documentation
To get started, visit our [wiki](https://github.com/manifoldjs/ManifoldJS/wiki).

## License

> manifoldjs-lib
> Copyright (c) Microsoft Corporation
> All rights reserved.
> MIT License
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
> THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
137 changes: 134 additions & 3 deletions lib/fileTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ var _mkdirp = require('mkdirp'),
ncp = require('ncp'),
Q = require('q');

var utils = require('./utils');
var log = require('./log'),
utils = require('./utils');

var stat = Q.nfbind(fs.stat);

function readFile (source, callback) {
return Q.nfcall(fs.readFile, source, 'utf8')
Expand Down Expand Up @@ -77,7 +80,7 @@ function mkdirp (filePath, callback) {
var rootPath = path.parse(fullPath).root;

// create directory recursively
return Q.nfcall(fs.stat, rootPath).then(function () {
return stat(rootPath).then(function () {
return Q.nfcall(_mkdirp, filePath);
})
.nodeify(callback);
Expand Down Expand Up @@ -125,12 +128,140 @@ function searchFile (dir, fileName, callback) {
});
}

// Copies the 'source' file to 'target' if it's missing after creating the
// required directory structure.
function syncFile (source, target, callback) {

// check target file
return stat(target).then(function (info) {
// verify that target is a file and not a directory
if (info.isDirectory()) {
return Q.reject(new Error('Cannot synchronize file \'' + source + '\'. There is already a directory in the target with the same name.'));
}

// skip target if it already exists
return;
})
.catch(function (err) {
// return failure for anything other than 'not found'
if (err.code !== 'ENOENT') {
return Q.reject(err);
}

// copy source to target
var targetDir = path.dirname(target);
return stat(targetDir).catch(function (err) {
// return failure for anything other than 'not found'
if (err.code !== 'ENOENT') {
return Q.reject(err);
}

// create target directory
return mkdirp(targetDir).then(function () {
log.debug('Created target directory at \'' + targetDir + '\'.');
})
.catch(function (err) {
// ignore error if target was already created by a different "thread"
if (err.code !== 'EEXIST') {
return Q.reject(err);
}
})
.then(function () {
return stat(targetDir);
});
})
.then(function () {
return copyFile(source, target).then(function () {
log.debug('Copied file \'' + source + '\' to \'' + target + '\'.');
return target;
});
});
})
.nodeify(callback);
}

// Copies all missing files and directories in 'source' to 'target'.
// Only checks that a file exists by comparing the file names, not
// that the contents of the source and target files the same.
// Returns an array with the list of files that were copied.
//
// NOTE: copyFiles in this module already provides similar functionality,
// but it's based on the 'ncp' package and does not provide the list of
// copied files.
function syncFiles (source, target, options, callback) {

if (arguments.length === 3 && utils.isFunction(options)) {
callback = options;
options = {};
}

// read the contents of the source directory
return Q.nfcall(fs.readdir, source).then(function (files) {

// process each file and folder
var tasks = files.map(function (fileOrDir) {
var sourceFile = path.join(source, fileOrDir);
return stat(sourceFile).then(function (info) {

// if fileOrDir is a directory, synchronize it
if (info.isDirectory()) {
return syncFiles(sourceFile, path.join(target, fileOrDir));
}

// synchronize a single file
var targetFile = path.join(target, fileOrDir);
return syncFile(sourceFile, targetFile);
});
});

// wait for all pending tasks to complete
return Q.all(tasks).then(function (values) {

// build a list of the files that were copied
return values.reduce(function (list, value) {
if (value) {
if (Array.isArray(value)) {
list.push.apply(list, value);
}
else {
list.push(value);
}
}

return list;
}, []);
});
})
.catch(function (err) {
if (err.code !== 'ENOTDIR') {
return Q.reject(err);
}

// specified source is a file not a directory
var sourceFile = path.basename(source);
var targetFile = path.basename(target);

// build target file path assuming target is a directory
// unless target already includes the file name
if (sourceFile !== targetFile) {
target = path.join(target, sourceFile);
}

// synchronize the file
return syncFile(source, target).then(function (file) {
return file ? [file] : [];
});
})
.nodeify(callback);
}

module.exports = {
readFile: readFile,
copyFile: copyFile,
copyFolder: copyFolder,
mkdirp: mkdirp,
createShortcut: createShortcut,
replaceFileContent: replaceFileContent,
searchFile: searchFile
searchFile: searchFile,
syncFiles: syncFiles
};
Loading

0 comments on commit 45ee7dd

Please sign in to comment.