Skip to content

Commit

Permalink
Add source extensions to app.json for Expo apps (default metro source…
Browse files Browse the repository at this point in the history
… exts + vue extensions)

Fixes #23

Addresses
- GeekyAnts/vue-native-core#183
- GeekyAnts/vue-native-core#194
  • Loading branch information
Rishabh Karnad committed Aug 19, 2019
1 parent 58df4ef commit b71d06d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"chalk": "^2.4.2",
"commander": "^2.20.0",
"lodash.union": "^4.6.0",
"ora": "^3.4.0",
"prompt": "^1.0.0",
"semver-regex": "^3.1.0"
Expand Down
31 changes: 25 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const spawnSync = require("child_process").spawnSync;
const program = require("commander");
const prompt = require("prompt");
const ora = require("ora");
const union = require("lodash.union")

const promptModule = require("./prompt/index");
const constantObjects = require("./utils/constants");
Expand Down Expand Up @@ -101,26 +102,26 @@ function init(projectName, cmd, useExpo) {
}
}

function createReactNativeCLIProject(projectName, cmd) {
async function createReactNativeCLIProject(projectName, cmd) {
const root = path.resolve(projectName);
if (fs.existsSync(projectName)) {
removeExistingDirectory(projectName);
}
console.log(chalk.green(`Creating Vue Native project ${chalk.bold(projectName)}\n`));
createRNProjectSync(projectName, cmd);
installPackages(projectName, cmd);
setupVueNativeApp(projectName, cmd);
await setupVueNativeApp(projectName, cmd);
}

function createExpoProject(projectName, cmd) {
async function createExpoProject(projectName, cmd) {
const root = path.resolve(projectName);
if (fs.existsSync(projectName)) {
removeExistingDirectory(projectName);
}
console.log(chalk.green(`Creating Vue Native project ${chalk.bold(projectName)}\n`));
createCrnaProjectSync(projectName, cmd);
installPackages(projectName, cmd);
setupVueNativeApp(projectName, cmd, true);
await setupVueNativeApp(projectName, cmd, true);
}

function createCrnaProjectSync(projectName, cmd) {
Expand Down Expand Up @@ -163,6 +164,17 @@ function removeExistingDirectory(directoryName) {
);
}

async function getSourceFileExtensions() {
const { getDefaultConfig } = require(`${process.cwd()}/node_modules/metro-config/src/index.js`);
const {
resolver: { sourceExts: defaultSourceExts }
} = await getDefaultConfig();

const sourceExts = union(defaultSourceExts, constantObjects.vueFileExtensions);

return sourceExts;
}

function installPackages(projectName, cmd) {
process.chdir(projectName);
installVueNativeDependency();
Expand Down Expand Up @@ -253,7 +265,7 @@ function getVueNativeDevDependencyPackageInstallationCommand() {
return vueNativePkgInstallationCommand;
}

function setupVueNativeApp(projectName, cmd, isCrna = false) {
async function setupVueNativeApp(projectName, cmd, isCrna = false) {
// process.chdir(projectName);
const rnCliFile = fs.readFileSync(
path.resolve(__dirname, "./utils/metro.config.js")
Expand All @@ -278,7 +290,14 @@ function setupVueNativeApp(projectName, cmd, isCrna = false) {
//
if (isCrna) {
const expoObj = JSON.parse(fs.readFileSync(path.join(constantObjects.appJsonPath), 'utf8'));
expoObj.expo.packagerOpts = { config: 'metro.config.js' };

const sourceExts = await getSourceFileExtensions();

expoObj.expo.packagerOpts = {
config: 'metro.config.js',
sourceExts: sourceExts,
};

fs.writeFileSync(
path.join(constantObjects.appJsonPath),
JSON.stringify(expoObj, null, 2)
Expand Down
8 changes: 4 additions & 4 deletions src/prompt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function promptForInvalidProjectName(
});
}

function createVueProjectAfterConfirmation(
async function createVueProjectAfterConfirmation(
promptObj,
onSuccessAnswer,
onFailedAnswer,
Expand All @@ -44,11 +44,11 @@ function createVueProjectAfterConfirmation(
default: "no"
};

promptObj.get(property, function(err, result) {
promptObj.get(property, async function(err, result) {
if (result.directoryExistAndContinue[0] === "y") {
onSuccessAnswer(name, cmd);
await onSuccessAnswer(name, cmd);
} else {
onFailedAnswer();
await onFailedAnswer();
}
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const constantObject = {
rnPkgCliFileName: "rn-cli.config.js",
metroConfigFile: "metro.config.js",
vueTransformerFileName: "vueTransformerPlugin.js",
appVueFileName: "App.vue"
appVueFileName: "App.vue",
expoAppJSONSourceExtsPath: "expo.packagerOpts.sourceExts",
vueFileExtensions: ["vue"],
};

module.exports = constantObject;
2 changes: 1 addition & 1 deletion src/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function getReactNativeCLIifAvailable() {
? `${constantObjects.rnPackageName} --version`
: `${constantObjects.rnPackageName} --version 2>/dev/null`

try {
try {
// execSync returns a Buffer -> convert to string
const commandResult = (execSync(processCommand).toString() || "").trim();
const regexMatches = commandResult.match(semverRegex());
Expand Down

0 comments on commit b71d06d

Please sign in to comment.