Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Merge requests
  • !1629
An error occurred while fetching the assigned milestone of the selected merge_request.

Ship a yarn.lock - WIP

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Administrator requested to merge github/fork/Conrad2134/ship-yarn-lock into master 8 years ago
  • Overview 9
  • Commits 4
  • Pipelines 0
  • Changes 2

Created by: Conrad2134

For #1551 (closed): Tried wrapping my head around this, and I'm stuck on how exactly we can generate a true lockfile since we're not testing a "released" version of react-scripts.

If anyone has a better grip on this than me, I'd love for you to set me straight!

Compare
  • master (base)

and
  • latest version
    dc2490c0
    4 commits, 2 years ago

2 files
+ 113
- 46

    Preferences

    File browser
    Compare changes
packages/cre‎ate-react-app‎
inde‎x.js‎ +112 -46
packag‎e.json‎ +1 -0
packages/create-react-app/index.js
+ 112
- 46
  • View file @ dc2490c0


@@ -58,6 +58,8 @@ var path = require('path');
@@ -58,6 +58,8 @@ var path = require('path');
var execSync = require('child_process').execSync;
var execSync = require('child_process').execSync;
var spawn = require('cross-spawn');
var spawn = require('cross-spawn');
var semver = require('semver');
var semver = require('semver');
 
var https = require('https');
 
var request = require('request');
var projectName;
var projectName;
@@ -145,12 +147,14 @@ function shouldUseYarn() {
@@ -145,12 +147,14 @@ function shouldUseYarn() {
}
}
}
}
function install(dependencies, verbose, callback) {
function install(dependencies, verbose, useYarnLock, callback) {
var command;
var command;
var args;
var args;
if (shouldUseYarn()) {
if (shouldUseYarn()) {
command = 'yarnpkg';
command = 'yarnpkg';
args = [ 'add', '--exact'].concat(dependencies);
 
// If the yarn.lock exists, just run `yarnpkg`.
 
args = useYarnLock ? [] : ['add', '--exact'].concat(dependencies);
} else {
} else {
checkNpmVersion();
checkNpmVersion();
command = 'npm';
command = 'npm';
@@ -173,6 +177,56 @@ function run(root, appName, version, verbose, originalDirectory, template) {
@@ -173,6 +177,56 @@ function run(root, appName, version, verbose, originalDirectory, template) {
var allDependencies = ['react', 'react-dom', packageToInstall];
var allDependencies = ['react', 'react-dom', packageToInstall];
 
var installDependencies = function(yarnLockExists) {
 
install(allDependencies, verbose, yarnLockExists, function(code, command, args) {
 
if (code !== 0) {
 
console.log();
 
console.error('Aborting installation.', chalk.cyan(command + ' ' + args.join(' ')), 'has failed.');
 
// On 'exit' we will delete these files from target directory.
 
var knownGeneratedFiles = [
 
'package.json', 'npm-debug.log', 'yarn-error.log', 'yarn-debug.log', 'node_modules'
 
];
 
var currentFiles = fs.readdirSync(path.join(root));
 
currentFiles.forEach(function (file) {
 
knownGeneratedFiles.forEach(function (fileToMatch) {
 
// This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
 
// and the rest of knownGeneratedFiles.
 
if ((fileToMatch.match(/.log/g) && file.indexOf(fileToMatch) === 0) || file === fileToMatch) {
 
console.log('Deleting generated file...', chalk.cyan(file));
 
fs.removeSync(path.join(root, file));
 
}
 
});
 
});
 
var remainingFiles = fs.readdirSync(path.join(root));
 
if (!remainingFiles.length) {
 
// Delete target folder if empty
 
console.log('Deleting', chalk.cyan(appName + '/'), 'from', chalk.cyan(path.resolve(root, '..')));
 
fs.removeSync(path.join(root));
 
}
 
console.log('Done.');
 
process.exit(1);
 
}
 
 
checkNodeVersion(packageName);
 
 
// Since react-scripts has been installed with --save
 
// We need to move it into devDependencies and rewrite package.json
 
if (!yarnLockExists) {
 
moveReactScriptsToDev(packageName);
 
}
 
 
var scriptsPath = path.resolve(
 
process.cwd(),
 
'node_modules',
 
packageName,
 
'scripts',
 
'init.js'
 
);
 
var init = require(scriptsPath);
 
init(root, appName, verbose, originalDirectory, template);
 
});
 
}
 
console.log('Installing packages. This might take a couple minutes.');
console.log('Installing packages. This might take a couple minutes.');
console.log(
console.log(
'Installing ' + chalk.cyan('react') + ', ' + chalk.cyan('react-dom') +
'Installing ' + chalk.cyan('react') + ', ' + chalk.cyan('react-dom') +
@@ -180,51 +234,43 @@ function run(root, appName, version, verbose, originalDirectory, template) {
@@ -180,51 +234,43 @@ function run(root, appName, version, verbose, originalDirectory, template) {
);
);
console.log();
console.log();
install(allDependencies, verbose, function(code, command, args) {
// Try to download the yarn.lock file.
if (code !== 0) {
if (shouldUseYarn()) {
console.log();
var releaseRequest = request({
console.error('Aborting installation.', chalk.cyan(command + ' ' + args.join(' ')), 'has failed.');
uri: 'https://api.github.com/repos/facebookincubator/create-react-app/releases/latest',
// On 'exit' we will delete these files from target directory.
method: 'GET',
var knownGeneratedFiles = [
headers: {
'package.json', 'npm-debug.log', 'yarn-error.log', 'yarn-debug.log', 'node_modules'
'User-Agent': 'create-react-app'
];
var currentFiles = fs.readdirSync(path.join(root));
currentFiles.forEach(function (file) {
knownGeneratedFiles.forEach(function (fileToMatch) {
// This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
// and the rest of knownGeneratedFiles.
if ((fileToMatch.match(/.log/g) && file.indexOf(fileToMatch) === 0) || file === fileToMatch) {
console.log('Deleting generated file...', chalk.cyan(file));
fs.removeSync(path.join(root, file));
}
});
});
var remainingFiles = fs.readdirSync(path.join(root));
if (!remainingFiles.length) {
// Delete target folder if empty
console.log('Deleting', chalk.cyan(appName + '/'), 'from', chalk.cyan(path.resolve(root, '..')));
fs.removeSync(path.join(root));
}
}
console.log('Done.');
}, function(err, response, releasesBody) {
process.exit(1);
var yarnLockUrl = JSON.parse(releasesBody).assets.reduce(function(acc, asset) {
}
return asset.name === 'yarn.lock' ? asset.browser_download_url : acc;
}, "");
checkNodeVersion(packageName);
// TODO: We'll want to copy the package.json && yarn.lock,
// Since react-scripts has been installed with --save
// `yarn install` will change the lockfile if the deps don't
// We need to move it into devDependencies and rewrite package.json
// satisfy versions in the package.json.
moveReactScriptsToDev(packageName);
if (!yarnLockUrl) {
var scriptsPath = path.resolve(
installDependencies(false);
process.cwd(),
'node_modules',
return;
packageName,
}
'scripts',
'init.js'
var yarnLockRequest = request(yarnLockUrl, function(err, response, body) {
);
fs.writeFileSync('yarn.lock', body); // TODO: Async
var init = require(scriptsPath);
init(root, appName, verbose, originalDirectory, template);
// Success. Write the package.json dependencies manually.
});
writePackageJsonFromYarnLock(packageName, packageToInstall);
 
installDependencies(true);
 
}).on('error', function(err) {
 
fs.unlink(dest);
 
installDependencies(false);
 
});
 
});
 
} else {
 
installDependencies(false);
 
}
}
}
function getInstallPackage(version) {
function getInstallPackage(version) {
@@ -352,6 +398,26 @@ function moveReactScriptsToDev(packageName) {
@@ -352,6 +398,26 @@ function moveReactScriptsToDev(packageName) {
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2));
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2));
}
}
 
// TODO: Get rid of most this - we'll copy a package.json
 
// Will need to write the name, though.
 
function writePackageJsonFromYarnLock(packageName, packageVersion) {
 
var packagePath = path.join(process.cwd(), 'package.json');
 
var packageJson = require(packagePath);
 
 
packageJson.devDependencies = packageJson.devDependencies || {};
 
packageJson.devDependencies[packageName] = packageVersion;
 
 
var reactVersion = 'latest';
 
var reactDomVersion = 'latest';
 
 
packageJson.dependencies = {
 
'react': reactVersion,
 
'react-dom': reactDomVersion
 
};
 
 
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2));
 
}
 
// If project only contains files generated by GH, it’s safe.
// If project only contains files generated by GH, it’s safe.
// We also special case IJ-based products .idea because it integrates with CRA:
// We also special case IJ-based products .idea because it integrates with CRA:
// https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-243446094
// https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-243446094
packages/create-react-app/package.json
+ 1
- 0
  • View file @ dc2490c0


@@ -24,6 +24,7 @@
@@ -24,6 +24,7 @@
"commander": "^2.9.0",
"commander": "^2.9.0",
"cross-spawn": "^4.0.0",
"cross-spawn": "^4.0.0",
"fs-extra": "^1.0.0",
"fs-extra": "^1.0.0",
 
"request": "^2.79.0",
"semver": "^5.0.3"
"semver": "^5.0.3"
}
}
}
}
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference:
Source branch: github/fork/Conrad2134/ship-yarn-lock

Menu

Explore Projects Groups Snippets