puppeteer/phantom_shim/FileSystem.js

376 lines
7.4 KiB
JavaScript
Raw Normal View History

2017-05-11 07:06:41 +00:00
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let path = require('path');
let fs = require('fs');
let deasync = require('deasync');
let removeRecursive = require('rimraf').sync;
let copyRecursive = deasync(require('ncp').ncp);
2017-05-11 07:06:41 +00:00
class FileSystem {
2017-06-21 20:51:06 +00:00
constructor() {
this.separator = path.sep;
}
2017-06-21 20:51:06 +00:00
/**
* @return {string}
*/
2017-06-21 20:51:06 +00:00
get workingDirectory() {
return process.cwd();
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} directoryPath
*/
2017-06-21 20:51:06 +00:00
changeWorkingDirectory(directoryPath) {
try {
process.chdir(directoryPath);
return true;
} catch (e){
return false;
}
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} relativePath
* @return {string}
*/
2017-06-21 20:51:06 +00:00
absolute(relativePath) {
relativePath = path.normalize(relativePath);
if (path.isAbsolute(relativePath))
return relativePath;
return path.resolve(path.join(process.cwd(), relativePath));
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
exists(filePath) {
return fs.existsSync(filePath);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} fromPath
* @param {string} toPath
*/
2017-06-21 20:51:06 +00:00
copy(fromPath, toPath) {
let content = fs.readFileSync(fromPath);
2017-06-21 20:51:06 +00:00
fs.writeFileSync(toPath, content);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} fromPath
* @param {string} toPath
*/
2017-06-21 20:51:06 +00:00
move(fromPath, toPath) {
let content = fs.readFileSync(fromPath);
2017-06-21 20:51:06 +00:00
fs.writeFileSync(toPath, content);
fs.unlinkSync(fromPath);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @return {number}
*/
2017-06-21 20:51:06 +00:00
size(filePath) {
return fs.statSync(filePath).size;
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
*/
2017-06-21 20:51:06 +00:00
touch(filePath) {
fs.closeSync(fs.openSync(filePath, 'a'));
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
*/
2017-06-21 20:51:06 +00:00
remove(filePath) {
fs.unlinkSync(filePath);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
lastModified(filePath) {
return fs.statSync(filePath).mtime;
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} dirPath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
makeDirectory(dirPath) {
try {
fs.mkdirSync(dirPath);
return true;
} catch (e) {
return false;
}
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} dirPath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
makeTree(dirPath) {
return this.makeDirectory(dirPath);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} dirPath
*/
2017-06-21 20:51:06 +00:00
removeTree(dirPath) {
removeRecursive(dirPath);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} fromPath
* @param {string} toPath
*/
2017-06-21 20:51:06 +00:00
copyTree(fromPath, toPath) {
copyRecursive(fromPath, toPath);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} dirPath
* @return {!Array<string>}
*/
2017-06-21 20:51:06 +00:00
list(dirPath) {
return fs.readdirSync(dirPath);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} linkPath
* @return {string}
*/
2017-06-21 20:51:06 +00:00
readLink(linkPath) {
return fs.readlinkSync(linkPath);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @param {Object} data
* @param {string} mode
*/
2017-06-21 20:51:06 +00:00
write(filePath, data, mode) {
let fd = new FileDescriptor(filePath, mode, 'utf8');
2017-06-21 20:51:06 +00:00
fd.write(data);
fd.close();
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} somePath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
isAbsolute(somePath) {
return path.isAbsolute(somePath);
}
2017-06-21 20:51:06 +00:00
/**
* @return {string}
*/
2017-06-21 20:51:06 +00:00
read(filePath) {
return fs.readFileSync(filePath, 'utf8');
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
isFile(filePath) {
return fs.existsSync(filePath) && fs.lstatSync(filePath).isFile();
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} dirPath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
isDirectory(dirPath) {
return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
isLink(filePath) {
return fs.existsSync(filePath) && fs.lstatSync(filePath).isSymbolicLink();
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
isReadable(filePath) {
try {
fs.accessSync(filePath, fs.constants.R_OK);
return true;
} catch (e) {
return false;
}
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
isWritable(filePath) {
try {
fs.accessSync(filePath, fs.constants.W_OK);
return true;
} catch (e) {
return false;
}
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
isExecutable(filePath) {
try {
fs.accessSync(filePath, fs.constants.X_OK);
return true;
} catch (e) {
return false;
}
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} somePath
* @return {!Array<string>}
*/
2017-06-21 20:51:06 +00:00
split(somePath) {
somePath = path.normalize(somePath);
if (somePath.endsWith(path.sep))
somePath = somePath.substring(0, somePath.length - path.sep.length);
return somePath.split(path.sep);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} path1
* @param {string} path2
* @return {string}
*/
2017-06-21 20:51:06 +00:00
join(...args) {
if (args[0] === '' && args.length > 1)
args[0] = path.sep;
args = args.filter(part => typeof part === 'string');
return path.join.apply(path, args);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @param {(string|!Object)} option
* @return {!FileDescriptor}
*/
2017-06-21 20:51:06 +00:00
open(filePath, option) {
if (typeof option === 'string')
return new FileDescriptor(filePath, option);
return new FileDescriptor(filePath, option.mode);
}
2017-05-11 07:06:41 +00:00
}
let fdwrite = deasync(fs.write);
let fdread = deasync(fs.read);
2017-05-11 07:06:41 +00:00
class FileDescriptor {
2017-06-21 20:51:06 +00:00
/**
* @param {string} filePath
* @param {string} mode
*/
2017-06-21 20:51:06 +00:00
constructor(filePath, mode) {
this._position = 0;
this._encoding = 'utf8';
if (mode === 'rb') {
this._mode = 'r';
this._encoding = 'latin1';
} else if (mode === 'wb' || mode === 'b') {
this._mode = 'w';
this._encoding = 'latin1';
} else if (mode === 'rw+') {
this._mode = 'a+';
this._position = fs.existsSync(filePath) ? fs.statSync(filePath).size : 0;
} else {
this._mode = mode;
}
this._fd = fs.openSync(filePath, this._mode);
}
/**
* @param {string} data
*/
2017-06-21 20:51:06 +00:00
write(data) {
let buffer = Buffer.from(data, this._encoding);
let written = fdwrite(this._fd, buffer, 0, buffer.length, this._position);
2017-06-21 20:51:06 +00:00
this._position += written;
}
2017-06-21 20:51:06 +00:00
getEncoding() {
return 'UTF-8';
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} data
*/
2017-06-21 20:51:06 +00:00
writeLine(data) {
this.write(data + '\n');
}
2017-06-21 20:51:06 +00:00
/**
* @param {number=} size
* @return {string}
*/
2017-06-21 20:51:06 +00:00
read(size) {
let position = this._position;
2017-06-21 20:51:06 +00:00
if (!size) {
size = fs.fstatSync(this._fd).size;
position = 0;
}
let buffer = new Buffer(size);
let bytesRead = fdread(this._fd, buffer, 0, size, position);
2017-06-21 20:51:06 +00:00
this._position += bytesRead;
return buffer.toString(this._encoding);
}
2017-06-21 20:51:06 +00:00
flush() {
// noop.
}
2017-06-21 20:51:06 +00:00
/**
* @param {number} position
*/
2017-06-21 20:51:06 +00:00
seek(position) {
this._position = position;
}
2017-06-21 20:51:06 +00:00
close() {
fs.closeSync(this._fd);
}
2017-06-21 20:51:06 +00:00
/**
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
atEnd() {
}
2017-05-11 07:06:41 +00:00
}
module.exports = FileSystem;