Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thick-kiwis-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-cli': patch
---

Uses native FS promise API rather than promisifying manually
35 changes: 18 additions & 17 deletions packages/cli/lib/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const { promisify } = require('util');
const fetch = require('isomorphic-unfetch');
const glob = promisify(require('glob').glob);
const gittar = require('gittar');
const fs = require('../fs');
const { existsSync, mkdirSync } = require('fs');
const { copyFile, mkdir, readFile, writeFile } = require('fs').promises;
const os = require('os');
const { green } = require('kleur');
const { resolve, join } = require('path');
Expand Down Expand Up @@ -148,7 +149,7 @@ async function updateTemplatesCache() {

try {
const repos = await fetch(TEMPLATES_REPO_URL).then(r => r.json());
await fs.writeFile(cacheFilePath, JSON.stringify(repos, null, 2), 'utf-8');
await writeFile(cacheFilePath, JSON.stringify(repos, null, 2), 'utf-8');
} catch (err) {
error(`\nFailed to update template cache\n ${err}`);
}
Expand All @@ -168,14 +169,14 @@ async function fetchTemplates() {
info('Fetching official templates:\n');

// check if `.cache` folder exists or not, and create if does not exists
if (!fs.existsSync(cacheFolder)) {
await fs.mkdir(cacheFolder);
if (!existsSync(cacheFolder)) {
await mkdir(cacheFolder);
}

// If cache file doesn't exist, then hit the API and fetch the data
if (!fs.existsSync(cacheFilePath)) {
if (!existsSync(cacheFilePath)) {
const repos = await fetch(TEMPLATES_REPO_URL).then(r => r.json());
await fs.writeFile(
await writeFile(
cacheFilePath,
JSON.stringify(repos, null, 2),
'utf-8'
Expand All @@ -186,7 +187,7 @@ async function fetchTemplates() {
updateTemplatesCache();

// fetch the API response from cache file
const templatesFromCache = await fs.readFile(cacheFilePath, 'utf-8');
const templatesFromCache = await readFile(cacheFilePath, 'utf-8');
const parsedTemplates = JSON.parse(templatesFromCache);
const officialTemplates = normalizeTemplatesResponse(parsedTemplates || []);

Expand All @@ -200,8 +201,8 @@ async function fetchTemplates() {
}

async function copyFileToDestination(srcPath, destPath, force = false) {
if (!fs.existsSync(destPath) || force) {
await fs.copyFile(srcPath, destPath);
if (!existsSync(destPath) || force) {
await copyFile(srcPath, destPath);
}
}

Expand Down Expand Up @@ -269,8 +270,8 @@ async function command(repo, dest, argv) {
info(`Assuming you meant ${repo}...`);
}

if (!fs.existsSync(resolve(cwd, dest, 'src'))) {
fs.mkdirSync(resolve(cwd, dest, 'src'), { recursive: true });
if (!existsSync(resolve(cwd, dest, 'src'))) {
mkdirSync(resolve(cwd, dest, 'src'), { recursive: true });
}

// Attempt to fetch the `template`
Expand Down Expand Up @@ -330,11 +331,11 @@ async function command(repo, dest, argv) {
entry,
enc = 'utf8';
for (entry of keeps) {
buf = await fs.readFile(entry, enc);
buf = await readFile(entry, enc);
dict.forEach((v, k) => {
buf = buf.replace(k, v);
});
await fs.writeFile(entry, buf, enc);
await writeFile(entry, buf, enc);
}
} else {
return error(`No \`template\` directory found within ${repo}!`, 1);
Expand All @@ -347,7 +348,7 @@ async function command(repo, dest, argv) {
pkgFile = resolve(target, 'package.json');

if (pkgFile) {
pkgData = JSON.parse(await fs.readFile(pkgFile));
pkgData = JSON.parse(await readFile(pkgFile));
// Write default "scripts" if none found
pkgData.scripts =
pkgData.scripts || (await addScripts(pkgData, target, isYarn));
Expand All @@ -362,12 +363,12 @@ async function command(repo, dest, argv) {
}
// Find a `manifest.json`; use the first match, if any
let files = await glob(target + '/**/manifest.json');
let manifest = files[0] && JSON.parse(await fs.readFile(files[0]));
let manifest = files[0] && JSON.parse(await readFile(files[0]));
if (manifest) {
spinner.text = 'Updating `name` within `manifest.json` file';
manifest.name = manifest.short_name = argv.name;
// Write changes to `manifest.json`
await fs.writeFile(files[0], JSON.stringify(manifest, null, 2));
await writeFile(files[0], JSON.stringify(manifest, null, 2));
if (argv.name.length > 12) {
// @see https://developer.chrome.com/extensions/manifest/name#short_name
process.stdout.write('\n');
Expand All @@ -377,7 +378,7 @@ async function command(repo, dest, argv) {

if (pkgData) {
// Assume changes were made ¯\_(ツ)_/¯
await fs.writeFile(pkgFile, JSON.stringify(pkgData, null, 2));
await writeFile(pkgFile, JSON.stringify(pkgData, null, 2));
}

const sourceDirectory = join(resolve(cwd, dest), 'src');
Expand Down
22 changes: 0 additions & 22 deletions packages/cli/lib/fs.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cli/lib/lib/webpack/run-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const webpack = require('webpack');
const getPort = require('get-port');
const { resolve } = require('path');
const clear = require('console-clear');
const { writeFile } = require('../../fs');
const { writeFile } = require('fs').promises;
const { bold, red, green, magenta } = require('kleur');
const DevServer = require('webpack-dev-server');
const clientConfig = require('./webpack-client-config');
Expand Down
9 changes: 5 additions & 4 deletions packages/cli/lib/lib/webpack/transform-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { resolve } = require('path');
const webpack = require('webpack');
const fs = require('../../fs');
const { statSync } = require('fs');
const { stat } = require('fs').promises;
const { error } = require('../../util');

const FILE = 'preact.config';
Expand All @@ -12,7 +13,7 @@ async function findConfig(env) {
let config = `${FILE}.${EXTENSIONS[idx]}`;
let path = resolve(env.cwd, config);
try {
await fs.stat(path);
await stat(path);
return { configFile: config, isDefault: true };
} catch (e) {}
}
Expand Down Expand Up @@ -99,7 +100,7 @@ module.exports = async function (env, webpackConfig, isServer = false) {
let myConfig = resolve(env.cwd, env.config);

try {
await fs.stat(myConfig);
await stat(myConfig);
} catch (e) {
if (isDefault) return;
throw new Error(
Expand Down Expand Up @@ -285,7 +286,7 @@ class WebpackConfigHelpers {
setHtmlTemplate(config, template) {
let isPath;
try {
fs.statSync(template);
statSync(template);
isPath = true;
} catch (e) {}

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/tests/build.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { join } = require('path');
const { readFile } = require('../lib/fs');
const { existsSync } = require('fs');
const { readFile } = require('fs').promises;
const looksLike = require('html-looks-like');
const { create, build } = require('./lib/cli');
const { snapshot, hasKey, isWithin } = require('./lib/utils');
const { existsSync } = require('fs');
const { subject } = require('./lib/output');
const images = require('./images/build');
const { promisify } = require('util');
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/tests/create.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs');
const { readFileSync } = require('fs');
const { relative, resolve } = require('path');
const { create } = require('./lib/cli');
const { expand } = require('./lib/utils');
Expand All @@ -24,7 +24,7 @@ describe('preact create', () => {
let dir = await create('netlify');

const templateFilePath = resolve(__dirname, dir, 'src', 'template.html');
const template = fs.readFileSync(templateFilePath).toString('utf8');
const template = readFileSync(templateFilePath).toString('utf8');

expect(template.includes('twitter:card')).toEqual(true);
});
Expand All @@ -33,7 +33,7 @@ describe('preact create', () => {
let dir = await create('simple');

const templateFilePath = resolve(__dirname, dir, 'src', 'template.html');
const template = fs.readFileSync(templateFilePath).toString('utf8');
const template = readFileSync(templateFilePath).toString('utf8');

expect(template.includes('apple-touch-icon')).toEqual(true);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/tests/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
const { relative, resolve } = require('path');
const { stat } = require('../../lib/fs');
const { stat } = require('fs').promises;
const minimatch = require('minimatch');
const pRetry = require('p-retry');
const { promisify } = require('util');
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/tests/service-worker.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { join } = require('path');
const { readFile, writeFile } = require('fs').promises;
const { create, build } = require('./lib/cli');
const { readFile, writeFile } = require('../lib/fs');
const { sleep } = require('./lib/utils');
const { getServer } = require('./server');
const startChrome = require('./lib/chrome');
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/tests/watch.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('../lib/fs');
const { readFile, writeFile } = require('fs').promises;
const { resolve } = require('path');
const startChrome = require('./lib/chrome');
const { create, watch } = require('./lib/cli');
Expand All @@ -22,9 +22,9 @@ describe('preact', () => {
let page = await loadPage(chrome, 'http://127.0.0.1:8083/');

let header = resolve(app, './src/components/header/index.js');
let original = await fs.readFile(header, 'utf8');
let original = await readFile(header, 'utf8');
let update = original.replace('<h1>Preact App</h1>', '<h1>Test App</h1>');
await fs.writeFile(header, update);
await writeFile(header, update);

await waitUntilExpression(
page,
Expand Down