Best JavaScript code snippet using wpt
file-utils.js
Source:file-utils.js
...94 * @trow Error95 */96 static createDirectories (createDirectories) {97 createDirectories.forEach(function (createDirectory) {98 FileUtils.createDirectory(createDirectory)99 })100 }101 /**102 * Create directory103 * @param failIfExists if true then error is returned if the directory already exists104 * @trow Error105 */106 static createDirectory (createDirectory) {107 if (createDirectory instanceof CreateDirectory) {108 const directoryExists = FileUtils.directoryExists(createDirectory.directory)109 if (directoryExists === false) {110 try {111 fs.mkdirSync(createDirectory.directory)112 } catch (error) {113 throw Errors.generalError("Failed to create directory: '" + createDirectory.directory + "'", error)114 }115 } else if (createDirectory.failIfExists === true) {116 throw Errors.generalError("Directory already exists: '" + createDirectory.directory + "'")117 }118 } else {119 throw Errors.invalidArgument('createDirectory', 'CreateDirectory')120 }121 }122 /**123 * Copy an array of files, this method does not undo previous copies if one copy fails124 * @param array of CopyFile objects125 * @throws Error126 */127 static copyFiles (copyFiles) {128 copyFiles.forEach(function (copyFile) {129 FileUtils.copyFile(copyFile)130 })131 }132 /**133 * Copy a file from source to destination134 * @param copyFile CopyFile object135 * @throw Error136 */137 static copyFile (copyFile) {138 if (copyFile instanceof CopyFile) {139 const fileExists = FileUtils.fileExists(copyFile.destination)140 if (fileExists === false) {141 try {142 const data = fs.readFileSync(copyFile.source, 'utf8')143 fs.writeFileSync(copyFile.destination, data, 'utf8')144 } catch (error) {145 throw Errors.generalError("Failed to copy file: '" + copyFile.source + "'", error)146 }147 } else if (copyFile.failIfExists === true) {148 throw Errors.generalError("File already exists: '" + copyFile.destination + "'")149 }150 } else {151 throw Errors.invalidArgument('copyFile', 'CopyFile')152 }153 }154 /**155 * Replace characters that are not allowed in a filename156 * @param fileName157 * @param replaceWithCharacter158 * @return string159 */160 static replaceIllegalFileNameCharacters (fileName, replaceWithCharacter) {161 return fileName.replace(/[|&;$%@"<> _()+,]/g, replaceWithCharacter)162 }163 /**164 * Copy source directory recursively to destination165 * @param {string} src The path to the thing to copy.166 * @param {string} dest The path to the new copy.167 */168 static copyRecusive (source, destination) {169 const exists = fs.existsSync(source)170 const stats = exists && fs.statSync(source)171 const isDirectory = exists && stats.isDirectory()172 if (exists && isDirectory) {173 FileUtils.createDirectory(new CreateDirectory(destination, false))174 fs.readdirSync(source).forEach(function (sourceItem) {175 FileUtils.copyRecusive(path.join(source, sourceItem), path.join(destination, sourceItem))176 })177 } else {178 FileUtils.copyFile(new CopyFile(source, destination, false))179 }180 }181}182module.exports = {183 FileUtils: FileUtils,184 CopyFile: CopyFile,185 CreateDirectory: CreateDirectory...
DirectoryWriter.test.ts
Source:DirectoryWriter.test.ts
...27\\---dir3/28`;29 const root = new DirectoryNode();30 root.addChildren([31 DirectoryNode.createDirectory('dir1', 0),32 DirectoryNode.createDirectory('dir2', 0),33 DirectoryNode.createDirectory('dir3', 0),34 ]);35 const result = directoryWriter.getDirectoryRepresentation(root);36 expect(result).toBe(expected);37 });38 test('One level directories and files', () => {39 const expected = `+---dir1/40+---dir2/41+---dir3/42+---file1.txt43\\---file2.txt44`;45 const root = new DirectoryNode();46 root.addChildren([47 DirectoryNode.createDirectory('dir1', 0),48 DirectoryNode.createDirectory('dir2', 0),49 DirectoryNode.createDirectory('dir3', 0),50 DirectoryNode.createFile('file1.txt', 0),51 DirectoryNode.createFile('file2.txt', 0),52 ]);53 const result = directoryWriter.getDirectoryRepresentation(root);54 expect(result).toBe(expected);55 });56 test('Nested directories with files', () => {57 const expected = `+---dir1/58| +---dir3/59| | \\---dir4/60| | \\---file2.txt61| \\---file1.txt62\\---dir2/63`;64 const root = new DirectoryNode();65 root.addChildren([66 DirectoryNode.createDirectory('dir1', 1, [67 DirectoryNode.createDirectory('dir3', 2, [68 DirectoryNode.createDirectory('dir4', 3, [DirectoryNode.createFile('file2.txt', 0)]),69 ]),70 DirectoryNode.createFile('file1.txt', 0),71 ]),72 DirectoryNode.createDirectory('dir2', 1, []),73 ]);74 const result = directoryWriter.getDirectoryRepresentation(root);75 expect(result).toBe(expected);76 });77 test('Nested directories with different charset', () => {78 directoryWriter = new DirectoryWriter(new TextDirectoryRepresentation(charsetUtf8));79 const expected = `ââââb/80 ââââbc/81 ââââbca/82 ââââbca-file1.txt83`;84 const root = new DirectoryNode();85 root.addChildren([86 DirectoryNode.createDirectory('b', 1, [87 DirectoryNode.createDirectory('bc', 2, [88 DirectoryNode.createDirectory('bca', 3, [DirectoryNode.createFile('bca-file1.txt', 0)]),89 ]),90 ]),91 ]);92 const result = directoryWriter.getDirectoryRepresentation(root);93 expect(result).toBe(expected);94 });95 test('Nested directories with not empty ones', () => {96 directoryWriter = new DirectoryWriter(new TextDirectoryRepresentation(charsetUtf8));97 const expected = `ââââa/98â ââââ...99ââââa1/100ââââb/101â ââââ...102ââââc/103ââââc1/104â ââââc2/105â ââââ...106ââââd/107 ââââ...108`;109 const root = new DirectoryNode();110 const directoryA = DirectoryNode.createDirectory('a', 0);111 const directoryB = DirectoryNode.createDirectory('b', 0);112 const directoryD = DirectoryNode.createDirectory('d', 0);113 const directoryC2 = DirectoryNode.createDirectory('c2', 0);114 directoryA.markDirectoryAsNoEmpty();115 directoryB.markDirectoryAsNoEmpty();116 directoryD.markDirectoryAsNoEmpty();117 directoryC2.markDirectoryAsNoEmpty();118 root.addChildren([119 directoryA,120 DirectoryNode.createDirectory('a1', 0),121 directoryB,122 DirectoryNode.createDirectory('c', 0),123 DirectoryNode.createDirectory('c1', 1, [directoryC2]),124 directoryD,125 ]);126 const result = directoryWriter.getDirectoryRepresentation(root);127 expect(result).toBe(expected);128 });129 });...
MakeProjectFolder.js
Source:MakeProjectFolder.js
1import System.IO;2//generate folders in our project3//add menu item4//generate folder and names from the script5@MenuItem("Project Tools / Make Folders") // The Menu item6//The first static function7static function MakeFolder(){8 GenerateFolders();9}10static function GenerateFolders(){11 var projectPath: String = Application.dataPath + "/"; //Stores path for folders12 Debug.Log("Folders Created!"); // Print to console of success13 Directory.CreateDirectory(projectPath + "Audio"); //Creates folders >>14 Directory.CreateDirectory(projectPath + "Materials");15 Directory.CreateDirectory(projectPath + "Meshes");16 Directory.CreateDirectory(projectPath + "Fonts");17 Directory.CreateDirectory(projectPath + "Textures");18 Directory.CreateDirectory(projectPath + "Resources");19 Directory.CreateDirectory(projectPath + "Scripts");20 Directory.CreateDirectory(projectPath + "Shaders");21 Directory.CreateDirectory(projectPath + "Packages");22 Directory.CreateDirectory(projectPath + "Physics");23 Directory.CreateDirectory(projectPath + "Prefabs");24 25 AssetDatabase.Refresh(); //Refreshes Project Panel...
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools();3wp.createDirectory('test', function(err, data){4 if(err){5 console.log(err);6 }else{7 console.log(data);8 }9});10var wptools = require('wptools');11var wp = new wptools();12wp.uploadFile('test', 'test.jpg', 'test.jpg', function(err, data){13 if(err){14 console.log(err);15 }else{16 console.log(data);17 }18});19var wptools = require('wptools');20var wp = new wptools();21wp.deleteFile('test.jpg', function(err, data){22 if(err){23 console.log(err);24 }else{25 console.log(data);26 }27});28var wptools = require('wptools');29var wp = new wptools();30wp.deleteDirectory('test', function(err, data){31 if(err){32 console.log(err);33 }else{34 console.log(data);35 }36});37var wptools = require('wptools');38var wp = new wptools();39wp.deleteDirectoryAndFiles('test', function(err, data){40 if(err){41 console.log(err);42 }else{43 console.log(data);44 }45});46var wptools = require('wptools');47var wp = new wptools();48wp.getFile('test.jpg', function(err, data){49 if(err){50 console.log(err);51 }else{52 console.log(data);53 }54});55var wptools = require('wptools');
Using AI Code Generation
1var wptools = require('wptools');2wptools.createDirectory('path/to/directory', function(error) {3 if (error) {4 console.log(error);5 } else {6 console.log('Directory Created');7 }8});9var wptools = require('wptools');10var error = wptools.createDirectorySync('path/to/directory');11if (error) {12 console.log(error);13} else {14 console.log('Directory Created');15}16var wptools = require('wptools');17wptools.removeDirectory('path/to/directory', function(error) {18 if (error) {19 console.log(error);20 } else {21 console.log('Directory Removed');22 }23});24var wptools = require('wptools');25var error = wptools.removeDirectorySync('path/to/directory');26if (error) {27 console.log(error);28} else {29 console.log('Directory Removed');30}31var wptools = require('wptools');32wptools.copyDirectory('path/to/source/directory', 'path/to/destination/directory', function(error) {33 if (error) {34 console.log(error);35 } else {36 console.log('Directory Copied');37 }38});39var wptools = require('wptools');40var error = wptools.copyDirectorySync('path/to/source/directory', 'path/to/destination/directory');41if (error) {42 console.log(error);43} else {44 console.log('Directory Copied');45}46var wptools = require('wptools');47wptools.moveDirectory('path/to/source/directory', 'path/to/destination/directory', function(error) {48 if (error) {49 console.log(error);50 } else {51 console.log('Directory Moved');52 }53});
Using AI Code Generation
1var wptoolbox = require('wptoolbox');2wptoolbox.createDirectory('test', function (err) {3 if (err) {4 console.log(err);5 }6});7var wptoolbox = require('wptoolbox');8wptoolbox.createDirectory('test', function (err) {9 if (err) {10 console.log(err);11 }12});13var wptoolbox = require('wptoolbox');14wptoolbox.createDirectory('test', function (err) {15 if (err) {16 console.log(err);17 }18});19var wptoolbox = require('wptoolbox');20wptoolbox.createDirectory('test', function (err) {21 if (err) {22 console.log(err);23 }24});25var wptoolbox = require('wptoolbox');26wptoolbox.createDirectory('test', function (err) {27 if (err) {28 console.log(err);29 }30});31var wptoolbox = require('wptoolbox');32wptoolbox.createDirectory('test', function (err) {33 if (err) {34 console.log(err);35 }36});37var wptoolbox = require('wptoolbox');38wptoolbox.createDirectory('test', function (err) {39 if (err) {40 console.log(err);41 }42});43var wptoolbox = require('wptoolbox');44wptoolbox.createDirectory('test', function (err) {45 if (err) {46 console.log(err);47 }48});
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools('Test');3wp.createDirectory(function(err, success){4 if(err){5 console.log(err);6 }7 else{8 console.log(success);9 }10});11var wptools = require('wptools');12var wp = wptools('Test');13wp.createDirectory().then(function(success){14 console.log(success);15}).catch(function(err){16 console.log(err);17});18var wptools = require('wptools');19var wp = wptools('Test');20wp.createDirectory().then(function(success){21 console.log(success);22}, function(err){23 console.log(err);24});25var wptools = require('wptools');26var wp = wptools('Test');27wp.createDirectory().then(function(success){28 console.log(success);29}).catch(function(err){30 console.log(err);31});32var wptools = require('wptools');33var wp = wptools('Test');34wp.createDirectory().then(function(success){35 console.log(success);36}, function(err){37 console.log(err);38});39var wptools = require('wptools');40var wp = wptools('Test');41wp.createDirectory().then(function(success){42 console.log(success);43}).catch(function(err){44 console.log(err);45});46var wptools = require('wptools');47var wp = wptools('Test');48wp.createDirectory().then(function(success){49 console.log(success);50}, function(err){51 console.log(err);52});53var wptools = require('wptools');54var wp = wptools('Test');55wp.createDirectory().then(function(success){56 console.log(success);57}).catch(function(err){58 console.log(err);59});
Using AI Code Generation
1var wpt = require('webpagetest');2var webpagetest = new wpt('API_KEY');3webpagetest.createDirectory('test', function(err, data) {4 console.log(data);5});6{7 response: {8 data: {9 data: {10 }11 }12 }13}14createDirectory(directoryName, callback)15webpagetest.createDirectory('test', function(err, data) {16 console.log(data);17});18{19 response: {20 data: {21 data: {22 }23 }24 }25}
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2wptoolkit.createDirectory('test', function(err, result) {3 if (err) {4 console.log('Error creating directory');5 } else {6 console.log('Directory created successfully');7 }8});9var wptoolkit = require('wptoolkit');10wptoolkit.deleteDirectory('test', function(err, result) {11 if (err) {12 console.log('Error deleting directory');13 } else {14 console.log('Directory deleted successfully');15 }16});17var wptoolkit = require('wptoolkit');18wptoolkit.createFile('test.txt', function(err, result) {19 if (err) {20 console.log('Error creating file');21 } else {22 console.log('File created successfully');23 }24});25var wptoolkit = require('wptoolkit');26wptoolkit.deleteFile('test.txt', function(err, result) {27 if (err) {28 console.log('Error deleting file');29 } else {30 console.log('File deleted successfully');31 }32});33var wptoolkit = require('wptoolkit');34wptoolkit.readFile('test.txt', function(err, result) {35 if (err) {36 console.log('Error reading file');37 } else {38 console.log('File read successfully');39 }40});41var wptoolkit = require('wptoolkit');42wptoolkit.writeFile('test.txt', 'Hello World', function(err, result) {43 if (err) {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!