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 readSuite( directory, file ) {
if ( !directory || typeof directory !== "string" ) {
throw new InvalidArgumentError( `Directory is empty or not a string` );
}
if ( !file || typeof file !== "string" ) {
throw new InvalidArgumentError( `File is empty or not a string` );
}
const filePath = join( directory, file );
// in case of snippets
if ( file === SNIPPETS_FILENAME && !fs.existsSync( filePath ) ) {
log.warn( `Suite file ${filePath} not found.` );
return null;
}
try {
const text = await readFile( filePath, "utf8" );
return parseJson( text, filePath );
} catch ( e ) {
log.warn( `Renderer process: io.readSuite: ${ e }` );
throw new IoError( `Suite file ${filePath} cannot be open.
Please make sure that the file exists and that you have read permission for it` );
export async function removeSuite( directory, file ) {
if ( !directory || typeof directory !== "string" ) {
throw new InvalidArgumentError( `Directory is empty or not a string` );
}
if ( !file || typeof file !== "string" ) {
throw new InvalidArgumentError( `File is empty or not a string` );
}
const filePath = join( directory, file );
try {
await unlink( filePath );
} catch ( e ) {
log.warn( `Renderer process: io.removeSuite: ${ e }` );
throw new IoError( `Suite file ${filePath} cannot be removed.` );
}
}
actions.saveSuite = ( options = {}, autosave = false ) => async ( dispatch, getState ) => {
const store = getState(),
{ projectDirectory } = store.settings,
{ filename } = { ...store.suite, ...options };
clearTimeout( autosaveTimeout );
try {
if ( !filename ) {
throw new InvalidArgumentError( "Empty suite filename" );
}
if ( !projectDirectory ) {
throw new InvalidArgumentError( "Empty project directory" );
}
const data = { ...store.suite, puppetry: version };
await writeSuite( projectDirectory, filename, JSON.stringify( data, null, " " ) );
await saveProject( store );
if ( !autosave ) {
dispatch( actions.updateSuite({ savedAt: dateToTs(), modified: false }) );
}
if ( store.suite.snippets ) {
await dispatch( snippetsActions.resetSnippets({
targets: store.suite.targets,
export async function writeSuite( directory, file, data ) {
if ( !directory || typeof directory !== "string" ) {
throw new InvalidArgumentError( `Directory is empty or not a string` );
}
if ( !file || typeof file !== "string" ) {
throw new InvalidArgumentError( `File is empty or not a string` );
}
const filePath = join( directory, file );
try {
await writeFile( filePath, data, "utf8" );
} catch ( e ) {
log.warn( `Renderer process: io.writeSuite: ${ e }` );
throw new IoError( `Suite file ${filePath} cannot be written.
Please make sure that you have write permission for it` );
}
}
actions.loadProject = ( directory = null ) => async ( dispatch, getState ) => {
const projectDirectory = directory || getState().settings.projectDirectory;
let project;
if ( !projectDirectory ) {
throw new InvalidArgumentError( "Empty project directory" );
}
try {
await dispatch( snippetsActions.loadSnippets() );
} catch ( e ) {
log.warn( `Renderer process: App.loadSnippets ${ e }` );
}
try {
dispatch( appActions.setApp({ loading: true }) );
project = await readProject( projectDirectory );
if ( typeof project === "undefined" ) {
return {};
}
ipcRenderer.send( E_PROJECT_LOADED, projectDirectory );
actions.removeSuite = ( filename ) => async ( dispatch, getState ) => {
if ( !filename || typeof filename !== "string" ) {
throw new InvalidArgumentError( "Filename is not a string or empty" );
}
const { projectDirectory } = getState().settings;
removeSuite( projectDirectory, filename );
};
actions.createSuite = ( rawFilename, title ) => async ( dispatch, getState ) => {
if ( !rawFilename || typeof rawFilename !== "string" ) {
throw new InvalidArgumentError( "Filename is not a string or empty" );
}
if ( !title || typeof title !== "string" ) {
throw new InvalidArgumentError( "Title is not a string or empty" );
}
const { projectDirectory } = getState().settings,
filename = normalizeFilename( rawFilename ) + ".json",
suite = { ...DEFAULT_STATE.suite, filename, title, savedAt: dateToTs() };
try {
if ( !projectDirectory ) {
throw new InvalidArgumentError( "Empty project directory" );
}
await writeSuite( projectDirectory, filename, JSON.stringify( suite, null, " " ) );
dispatch( actions.openSuiteFile( filename ) );
} catch ( e ) {
dispatch( errorActions.setError({
visible: true,
message: "Internal Error",
description: e.message
}) );
}
};
actions.createSuite = ( rawFilename, title ) => async ( dispatch, getState ) => {
if ( !rawFilename || typeof rawFilename !== "string" ) {
throw new InvalidArgumentError( "Filename is not a string or empty" );
}
if ( !title || typeof title !== "string" ) {
throw new InvalidArgumentError( "Title is not a string or empty" );
}
const { projectDirectory } = getState().settings,
filename = normalizeFilename( rawFilename ) + ".json",
suite = { ...DEFAULT_STATE.suite, filename, title, savedAt: dateToTs() };
try {
if ( !projectDirectory ) {
throw new InvalidArgumentError( "Empty project directory" );
}
await writeSuite( projectDirectory, filename, JSON.stringify( suite, null, " " ) );
dispatch( actions.openSuiteFile( filename ) );
} catch ( e ) {
dispatch( errorActions.setError({
visible: true,
message: "Internal Error",
description: e.message