Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function buildTypeScript(protoFiles: string[]) {
const tempDir = await createTempDir();
try {
// Use pbjs to generate static JS code for the protobuf definitions
const jsFile = await call(tempDir.name, pbjsMain, protoFiles, 'js', ['keep-case'], {
target: 'static-module',
wrap: 'commonjs',
});
const jsonDescriptor = await call(tempDir.name, pbjsMain, protoFiles, 'js', ['keep-case'], {
target: 'json',
});
const root = protobuf.loadSync(jsonDescriptor);
const js = transformJavaScriptSource(await fs.readFile(jsFile, 'utf8'), root);
await fs.writeFile(jsFile, js);
// Create TypeScript file
const tsFile = await call(tempDir.name, pbtsMain, [jsFile], 'ts');
return transformTypeScriptSource(await fs.readFile(tsFile, 'utf8'));
} finally {
tempDir.removeCallback();
}
}
private static async modifyWebpackConfig(packageJson: Package): Promise {
console.log('modifying webpack config');
try {
let originalConfig = await fs.readFile(this.copyConfigPath, 'utf-8');
let nativeDependencies = ['fs', 'child_process', 'electron', 'path', 'assert', 'cluster', 'crypto', 'dns', 'domain', 'events', 'http', 'https', 'net', 'os', 'process', 'punycode',
'querystring', 'readline', 'repl', 'stream', 'string_decoder', 'tls', 'tty', 'dgram', 'url', 'util', 'v8', 'vm', 'zlib'];
if (packageJson.nativeModules) {
nativeDependencies = nativeDependencies.concat(packageJson.nativeModules);
}
let externalsTemplate = await fs.readFile(path.join(__dirname, '..', 'res', 'externals.template'), 'utf-8');
let externals = externalsTemplate.replace('{ignores}', JSON.stringify(nativeDependencies));
let newConfig = originalConfig.replace(/return ?{/g, `return {\n${externals}`);
await fs.writeFile(this.configPath, newConfig);
console.log(chalk.green('finished modifying webpack config'));
}
catch (error) {
console.log(chalk.red('failed modifying webpack config'));
console.log(chalk.red(error));
process.exit(1);
}
}
it('deleteIfExists resolves when file deleted', () => fs.writeFile('test1', 'Hey there!')
.then(() => checkExists('test1'))
.then((exists) => {
if (!exists) {
throw new Error();
} else {
return deleteIfExists('test1');
}
}));
});
private static async createElectronEntryPoint(): Promise {
console.log('creating entry point');
var targetDir = path.join('src', 'electron');
if (!fs.existsSync(targetDir)){
fs.mkdirSync(targetDir);
}
let sourcePath = path.join(__dirname, '..', 'res', 'electron-main.js.template');
let targetPath = path.join(targetDir, 'electron.js');
let template = await fs.readFile(sourcePath, 'utf-8');
await fs.writeFile(targetPath, template);
}
async createReloadManagerExtension() {
const tmpDir = new TempDir();
await tmpDir.create();
this.registerCleanup(() => tmpDir.remove());
const extPath = path.join(
tmpDir.path(),
`reload-manager-extension-${Date.now()}`
);
log.debug(`Creating reload-manager-extension in ${extPath}`);
await asyncMkdirp(extPath);
await fs.writeFile(
path.join(extPath, 'manifest.json'),
JSON.stringify({
manifest_version: 2,
name: 'web-ext Reload Manager Extension',
version: '1.0',
permissions: ['management', 'tabs'],
background: {
scripts: ['bg.js'],
},
})
);
const wssInfo = this.wss.address();
const bgPage = `(function bgPage() {
async function getAllDevExtensions() {
export async function saveIdToSourceDir(
sourceDir: string, id: string
): Promise {
const filePath = path.join(sourceDir, extensionIdFile);
await fs.writeFile(filePath, [
'# This file was created by https://github.com/mozilla/web-ext',
'# Your auto-generated extension ID for addons.mozilla.org is:',
id.toString(),
].join('\n'));
log.debug(`Saved auto-generated ID ${id} to ${filePath}`);
}
export const writeSingleTypeScriptConfig = async ({ fileName, project, sourceFiles, improvements }: SingleTypeScriptConfigSettings) => {
await fs.writeFile(
fileName,
JSON.stringify(
{
fixes: printImprovements(improvements),
...(sourceFiles && { include: [sourceFiles] }),
projectPath: project,
},
undefined,
4,
),
);
};