@@ -3,7 +3,8 @@ const { promisify } = require('util');
33const fetch = require ( 'isomorphic-unfetch' ) ;
44const glob = promisify ( require ( 'glob' ) . glob ) ;
55const gittar = require ( 'gittar' ) ;
6- const fs = require ( '../fs' ) ;
6+ const { existsSync, mkdirSync } = require ( 'fs' ) ;
7+ const { copyFile, mkdir, readFile, writeFile } = require ( 'fs' ) . promises ;
78const os = require ( 'os' ) ;
89const { green } = require ( 'kleur' ) ;
910const { resolve, join } = require ( 'path' ) ;
@@ -148,7 +149,7 @@ async function updateTemplatesCache() {
148149
149150 try {
150151 const repos = await fetch ( TEMPLATES_REPO_URL ) . then ( r => r . json ( ) ) ;
151- await fs . writeFile ( cacheFilePath , JSON . stringify ( repos , null , 2 ) , 'utf-8' ) ;
152+ await writeFile ( cacheFilePath , JSON . stringify ( repos , null , 2 ) , 'utf-8' ) ;
152153 } catch ( err ) {
153154 error ( `\nFailed to update template cache\n ${ err } ` ) ;
154155 }
@@ -168,14 +169,14 @@ async function fetchTemplates() {
168169 info ( 'Fetching official templates:\n' ) ;
169170
170171 // check if `.cache` folder exists or not, and create if does not exists
171- if ( ! fs . existsSync ( cacheFolder ) ) {
172- await fs . mkdir ( cacheFolder ) ;
172+ if ( ! existsSync ( cacheFolder ) ) {
173+ await mkdir ( cacheFolder ) ;
173174 }
174175
175176 // If cache file doesn't exist, then hit the API and fetch the data
176- if ( ! fs . existsSync ( cacheFilePath ) ) {
177+ if ( ! existsSync ( cacheFilePath ) ) {
177178 const repos = await fetch ( TEMPLATES_REPO_URL ) . then ( r => r . json ( ) ) ;
178- await fs . writeFile (
179+ await writeFile (
179180 cacheFilePath ,
180181 JSON . stringify ( repos , null , 2 ) ,
181182 'utf-8'
@@ -186,7 +187,7 @@ async function fetchTemplates() {
186187 updateTemplatesCache ( ) ;
187188
188189 // fetch the API response from cache file
189- const templatesFromCache = await fs . readFile ( cacheFilePath , 'utf-8' ) ;
190+ const templatesFromCache = await readFile ( cacheFilePath , 'utf-8' ) ;
190191 const parsedTemplates = JSON . parse ( templatesFromCache ) ;
191192 const officialTemplates = normalizeTemplatesResponse ( parsedTemplates || [ ] ) ;
192193
@@ -200,8 +201,8 @@ async function fetchTemplates() {
200201}
201202
202203async function copyFileToDestination ( srcPath , destPath , force = false ) {
203- if ( ! fs . existsSync ( destPath ) || force ) {
204- await fs . copyFile ( srcPath , destPath ) ;
204+ if ( ! existsSync ( destPath ) || force ) {
205+ await copyFile ( srcPath , destPath ) ;
205206 }
206207}
207208
@@ -269,8 +270,8 @@ async function command(repo, dest, argv) {
269270 info ( `Assuming you meant ${ repo } ...` ) ;
270271 }
271272
272- if ( ! fs . existsSync ( resolve ( cwd , dest , 'src' ) ) ) {
273- fs . mkdirSync ( resolve ( cwd , dest , 'src' ) , { recursive : true } ) ;
273+ if ( ! existsSync ( resolve ( cwd , dest , 'src' ) ) ) {
274+ mkdirSync ( resolve ( cwd , dest , 'src' ) , { recursive : true } ) ;
274275 }
275276
276277 // Attempt to fetch the `template`
@@ -330,11 +331,11 @@ async function command(repo, dest, argv) {
330331 entry ,
331332 enc = 'utf8' ;
332333 for ( entry of keeps ) {
333- buf = await fs . readFile ( entry , enc ) ;
334+ buf = await readFile ( entry , enc ) ;
334335 dict . forEach ( ( v , k ) => {
335336 buf = buf . replace ( k , v ) ;
336337 } ) ;
337- await fs . writeFile ( entry , buf , enc ) ;
338+ await writeFile ( entry , buf , enc ) ;
338339 }
339340 } else {
340341 return error ( `No \`template\` directory found within ${ repo } !` , 1 ) ;
@@ -347,7 +348,7 @@ async function command(repo, dest, argv) {
347348 pkgFile = resolve ( target , 'package.json' ) ;
348349
349350 if ( pkgFile ) {
350- pkgData = JSON . parse ( await fs . readFile ( pkgFile ) ) ;
351+ pkgData = JSON . parse ( await readFile ( pkgFile ) ) ;
351352 // Write default "scripts" if none found
352353 pkgData . scripts =
353354 pkgData . scripts || ( await addScripts ( pkgData , target , isYarn ) ) ;
@@ -362,12 +363,12 @@ async function command(repo, dest, argv) {
362363 }
363364 // Find a `manifest.json`; use the first match, if any
364365 let files = await glob ( target + '/**/manifest.json' ) ;
365- let manifest = files [ 0 ] && JSON . parse ( await fs . readFile ( files [ 0 ] ) ) ;
366+ let manifest = files [ 0 ] && JSON . parse ( await readFile ( files [ 0 ] ) ) ;
366367 if ( manifest ) {
367368 spinner . text = 'Updating `name` within `manifest.json` file' ;
368369 manifest . name = manifest . short_name = argv . name ;
369370 // Write changes to `manifest.json`
370- await fs . writeFile ( files [ 0 ] , JSON . stringify ( manifest , null , 2 ) ) ;
371+ await writeFile ( files [ 0 ] , JSON . stringify ( manifest , null , 2 ) ) ;
371372 if ( argv . name . length > 12 ) {
372373 // @see https://developer.chrome.com/extensions/manifest/name#short_name
373374 process . stdout . write ( '\n' ) ;
@@ -377,7 +378,7 @@ async function command(repo, dest, argv) {
377378
378379 if ( pkgData ) {
379380 // Assume changes were made ¯\_(ツ)_/¯
380- await fs . writeFile ( pkgFile , JSON . stringify ( pkgData , null , 2 ) ) ;
381+ await writeFile ( pkgFile , JSON . stringify ( pkgData , null , 2 ) ) ;
381382 }
382383
383384 const sourceDirectory = join ( resolve ( cwd , dest ) , 'src' ) ;
0 commit comments