本文整理汇总了TypeScript中graceful-fs.readFileSync函数的典型用法代码示例。如果您正苦于以下问题:TypeScript readFileSync函数的具体用法?TypeScript readFileSync怎么用?TypeScript readFileSync使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了readFileSync函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: _loadModule
private _loadModule(
localModule: InitialModule,
from: Config.Path,
moduleName: string | undefined,
modulePath: Config.Path,
options: InternalModuleOptions | undefined,
moduleRegistry: ModuleRegistry,
) {
if (path.extname(modulePath) === '.json') {
const text = stripBOM(fs.readFileSync(modulePath, 'utf8'));
const transformedFile = this._scriptTransformer.transformJson(
modulePath,
this._getFullTransformationOptions(options),
text,
);
localModule.exports = this._environment.global.JSON.parse(
transformedFile,
);
} else if (path.extname(modulePath) === '.node') {
localModule.exports = require(modulePath);
} else {
// Only include the fromPath if a moduleName is given. Else treat as root.
const fromPath = moduleName ? from : null;
this._execModule(localModule, options, moduleRegistry, fromPath);
}
localModule.loaded = true;
}
示例2: catch
const readCacheFile = (cachePath: Config.Path): string | null => {
if (!fs.existsSync(cachePath)) {
return null;
}
let fileData;
try {
fileData = fs.readFileSync(cachePath, 'utf8');
} catch (e) {
e.message =
'jest: failed to read cache file: ' +
cachePath +
'\nFailure message: ' +
e.message;
removeFile(cachePath);
throw e;
}
if (fileData == null) {
// We must have somehow created the file but failed to write to it,
// let's delete it and retry.
removeFile(cachePath);
}
return fileData;
};
示例3: getServer
function getServer(): any {
if (config.get("protocol") === "https" ) {
let certificate = fs.readFileSync(config.get("ssl_cert") as string);
let privateKey = fs.readFileSync(config.get("ssl_key") as string);
console.log("creating https server");
let server = https.createServer({key: privateKey, cert: certificate}, app);
server.setTimeout(0);
return server;
}
else {
console.log("creating http server");
let server = http.createServer(app);
server.setTimeout(0);
return server;
}
}
示例4:
const getContent = (): string => {
if (content === undefined) {
content = fs.readFileSync(filePath, 'utf8');
}
return content;
};
示例5: getSha1
export async function getSha1(data: WorkerMessage): Promise<WorkerMetadata> {
const sha1 = data.computeSha1
? sha1hex(fs.readFileSync(data.filePath))
: null;
return {
dependencies: undefined,
id: undefined,
module: undefined,
sha1,
};
}
示例6: catch
retrieveSourceMap: source => {
const sourceMaps = runtime && runtime.getSourceMaps();
const sourceMapSource = sourceMaps && sourceMaps[source];
if (sourceMapSource) {
try {
return {
map: JSON.parse(fs.readFileSync(sourceMapSource, 'utf8')),
url: source,
};
} catch (e) {}
}
return null;
},
示例7: default
export default (level: number, sourceMaps?: SourceMapRegistry | null) => {
const levelAfterThisCall = level + 1;
const stack = callsites()[levelAfterThisCall];
const sourceMapFileName = sourceMaps && sourceMaps[stack.getFileName() || ''];
if (sourceMapFileName) {
try {
const sourceMap = fs.readFileSync(sourceMapFileName, 'utf8');
// @ts-ignore: Not allowed to pass string
addSourceMapConsumer(stack, new SourceMapConsumer(sourceMap));
} catch (e) {
// ignore
}
}
return stack;
};
示例8: requireModule
requireModule(
from: Config.Path,
moduleName?: string,
options?: InternalModuleOptions,
isRequireActual?: boolean | null,
) {
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
);
let modulePath;
// Some old tests rely on this mocking behavior. Ideally we'll change this
// to be more explicit.
const moduleResource = moduleName && this._resolver.getModule(moduleName);
const manualMock =
moduleName && this._resolver.getMockModule(from, moduleName);
if (
(!options || !options.isInternalModule) &&
!isRequireActual &&
!moduleResource &&
manualMock &&
manualMock !== this._isCurrentlyExecutingManualMock &&
this._explicitShouldMock[moduleID] !== false
) {
modulePath = manualMock;
}
if (moduleName && this._resolver.isCoreModule(moduleName)) {
return this._requireCoreModule(moduleName);
}
if (!modulePath) {
modulePath = this._resolveModule(from, moduleName);
}
let moduleRegistry;
if (!options || !options.isInternalModule) {
if (this._moduleRegistry[modulePath] || !this._isolatedModuleRegistry) {
moduleRegistry = this._moduleRegistry;
} else {
moduleRegistry = this._isolatedModuleRegistry;
}
} else {
moduleRegistry = this._internalModuleRegistry;
}
if (!moduleRegistry[modulePath]) {
// We must register the pre-allocated module object first so that any
// circular dependencies that may arise while evaluating the module can
// be satisfied.
const localModule: InitialModule = {
children: [],
exports: {},
filename: modulePath,
id: modulePath,
loaded: false,
};
moduleRegistry[modulePath] = localModule;
if (path.extname(modulePath) === '.json') {
localModule.exports = this._environment.global.JSON.parse(
stripBOM(fs.readFileSync(modulePath, 'utf8')),
);
} else if (path.extname(modulePath) === '.node') {
localModule.exports = require(modulePath);
} else {
// Only include the fromPath if a moduleName is given. Else treat as root.
const fromPath = moduleName ? from : null;
this._execModule(localModule, options, moduleRegistry, fromPath);
}
localModule.loaded = true;
}
return moduleRegistry[modulePath].exports;
}
示例9: runTestInternal
// Keeping the core of "runTest" as a separate function (as "runTestInternal")
// is key to be able to detect memory leaks. Since all variables are local to
// the function, when "runTestInternal" finishes its execution, they can all be
// freed, UNLESS something else is leaking them (and that's why we can detect
// the leak!).
//
// If we had all the code in a single function, we should manually nullify all
// references to verify if there is a leak, which is not maintainable and error
// prone. That's why "runTestInternal" CANNOT be inlined inside "runTest".
async function runTestInternal(
path: Config.Path,
globalConfig: Config.GlobalConfig,
config: Config.ProjectConfig,
resolver: Resolver,
context?: TestRunnerContext,
): Promise<RunTestInternalResult> {
const testSource = fs.readFileSync(path, 'utf8');
const parsedDocblock = docblock.parse(docblock.extract(testSource));
const customEnvironment = parsedDocblock['jest-environment'];
let testEnvironment = config.testEnvironment;
if (customEnvironment) {
if (Array.isArray(customEnvironment)) {
throw new Error(
`You can only define a single test environment through docblocks, got "${customEnvironment.join(
', ',
)}"`,
);
}
testEnvironment = getTestEnvironment({
...config,
testEnvironment: customEnvironment,
});
}
const TestEnvironment: typeof JestEnvironment = require(testEnvironment);
const testFramework: TestFramework =
process.env.JEST_CIRCUS === '1'
? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies
: require(config.testRunner);
const Runtime: typeof RuntimeClass = config.moduleLoader
? require(config.moduleLoader)
: require('jest-runtime');
let runtime: RuntimeClass | undefined = undefined;
const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout;
const consoleFormatter = (type: LogType, message: LogMessage) =>
getConsoleOutput(
config.cwd,
!!globalConfig.verbose,
// 4 = the console call is buried 4 stack frames deep
BufferedConsole.write(
[],
type,
message,
4,
runtime && runtime.getSourceMaps(),
),
);
let testConsole;
if (globalConfig.silent) {
testConsole = new NullConsole(consoleOut, process.stderr, consoleFormatter);
} else if (globalConfig.verbose) {
testConsole = new CustomConsole(
consoleOut,
process.stderr,
consoleFormatter,
);
} else {
testConsole = new BufferedConsole(() => runtime && runtime.getSourceMaps());
}
const environment = new TestEnvironment(config, {
console: testConsole,
testPath: path,
});
const leakDetector = config.detectLeaks
? new LeakDetector(environment)
: null;
const cacheFS = {[path]: testSource};
setGlobal(environment.global, 'console', testConsole);
runtime = new Runtime(config, environment, resolver, cacheFS, {
changedFiles: context && context.changedFiles,
collectCoverage: globalConfig.collectCoverage,
collectCoverageFrom: globalConfig.collectCoverageFrom,
collectCoverageOnlyFrom: globalConfig.collectCoverageOnlyFrom,
});
const start = Date.now();
const sourcemapOptions: SourceMapOptions = {
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap: source => {
//.........这里部分代码省略.........
示例10: express
import cassandraSeed from './cassandra-db/seed';
import mongoSeed from './mongo-db/seed';
import sqlSeed from './sql-db/seed';
import {connect, disconnect} from './db-connect';
const isSecure = config.https_secure && (process.env.NODE_ENV === 'production' || !process.env.NODE_ENV);
// Initialize express
let app = express();
// Initialize http server
let server: any = http.createServer(app);
// If specified in the default assets, https will be used
if (isSecure) {
let credentials = {
key: fs.readFileSync(config.key_loc, 'utf8'),
cert: fs.readFileSync(config.cert_loc, 'utf8')
};
server = https.createServer(credentials, app);
}
// Initialize the socketio with the respective server
let socketio = require('socket.io')(server, {
// serveClient: process.env.NODE_ENV !== 'production',
path: '/socket.io-client'
});
connect().subscribe(
x => {},
err => console.log(err),
() => {