Skip to content

Commit

Permalink
fix: unlink & sync-build problems
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmet-erim committed Aug 3, 2019
1 parent f2a39ad commit 3a0f144
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "symlink-manager",
"version": "1.2.2",
"version": "1.3.0",
"description": "Symlink Manager easily manage to symbolic link processes of your dependency packages.",
"main": "index.js",
"scripts": {
Expand Down
128 changes: 65 additions & 63 deletions src/angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,89 +82,91 @@ export default async function(options) {
selectedPackages.forEach(async packName => {
const { projectName, outputFolderPath, root } = libraries.find(library => library.packageName === packName);
if (options.command === 'link' || options.command === 'copy') {
try {
spinner.start();
spinner.start();

try {
if (options.syncBuild) {
execa.sync(packageManager, [...(packageManager === 'npm' ? ['run'] : []), 'ng', 'build', projectName]);
} else {
await build(packageManager, projectName);
}
} catch (error) {
spinner.stop();
Log.error(error.stderr);
process.exit(1);
try {
if (options.syncBuild) {
execa.sync(packageManager, [...(packageManager === 'npm' ? ['run'] : []), 'ng', 'build', projectName]);
} else {
await build(packageManager, projectName);
}
} catch (error) {
spinner.stop();
Log.error(error.stderr);
return;
}

if (options.command === 'link') {
if (options.command === 'link') {
if (options.syncBuild) {
execa.sync(packageManager, ['link'], { cwd: outputFolderPath });
execa.sync(packageManager, ['link', packName]);
} else {
await execa(packageManager, ['link'], { cwd: outputFolderPath });
await execa(packageManager, ['link', packName]);
} else if (options.command === 'copy') {
}
} else if (options.command === 'copy') {
if (options.syncBuild) {
copy(outputFolderPath, true);
} else {
await copy(outputFolderPath);
}
}

spinner.stop();
spinner.stop();

Log.success(`\n${packName} successfully built.`);
Log.success(`\n${packName} successfully built.`);

if (options.command === 'link') {
Log.success(`Symbolic link to ${packName} is successfully created.`);
} else if (options.command === 'copy') {
Log.success(`${packName} is successfully copied to node_modules.`);
}

if (options.noWatch) return;
Log.info(`${packName} is watching...`);
let destroy$ = new Subject();
let subscribe = {};
chokidar.watch(root, { ignored: /node_modules/ }).on(
'change',
_.debounce(async () => {
if (subscribe.closed === false) {
destroy$.next();
Log.info(`${packName} build process stopped.`);
}

Log.info(`\n${packName} build has been started.`);

subscribe = from(build(packageManager, projectName))
.pipe(
switchMap(() => (options.command === 'copy' ? from(copy(outputFolderPath)) : of(null))),
takeUntil(destroy$),
take(1),
)
.subscribe({
next: () => {
Log.success(`${packName} successfully built.`);
if (options.command === 'copy') {
Log.success(`The output files successfully copied.`);
}
},
error: error => {
Log.error(error.stderr);
},
});
}, 200),
);
} catch (err) {
spinner.stop();
Log.error(err);
process.exit(1);
if (options.command === 'link') {
Log.success(`Symbolic link to ${packName} is successfully created.`);
} else if (options.command === 'copy') {
Log.success(`${packName} is successfully copied to node_modules.`);
}

if (options.noWatch) return;
Log.info(`${packName} is watching...`);
let destroy$ = new Subject();
let subscribe = {};
chokidar.watch(root, { ignored: /node_modules/ }).on(
'change',
_.debounce(async () => {
if (subscribe.closed === false) {
destroy$.next();
Log.info(`${packName} build process stopped.`);
}

Log.info(`\n${packName} build has been started.`);

subscribe = from(build(packageManager, projectName))
.pipe(
switchMap(() => (options.command === 'copy' ? from(copy(outputFolderPath)) : of(null))),
takeUntil(destroy$),
take(1),
)
.subscribe({
next: () => {
Log.success(`${packName} successfully built.`);
if (options.command === 'copy') {
Log.success(`The output files successfully copied.`);
}
},
error: error => {
Log.error(error.stderr);
},
});
}, 200),
);
} else {
try {
spinner.start();

await execa(packageManager, ['unlink', packName]);
await execa(packageManager, ['unlink'], { cwd: outputFolderPath });

spinner.stop();
Log.info(`\nSymbolic link to ${packName} is successfully deleted.`);
} catch (error) {
} catch (err) {
spinner.stop();
Log.error(error);
process.exit(1);
Log.primary(`\n${packName} have not symbolic link`);
return;
}
}
});
Expand Down
23 changes: 16 additions & 7 deletions src/link-with-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,30 @@ export default async function(options, config) {
} catch (error) {
spinner.stop();
Log.error(error.stderr);
process.exit(1);
return;
}
}

try {
if (options.command === 'link') {
await execa(packageManager, ['link'], { cwd: pack.linkFolderPath });
await execa(packageManager, ['link', packName]);
if (options.syncBuild) {
execa.sync(packageManager, ['link'], { cwd: pack.linkFolderPath });
execa.sync(packageManager, ['link', packName]);
} else {
await execa(packageManager, ['link'], { cwd: pack.linkFolderPath });
await execa(packageManager, ['link', packName]);
}
} else if (options.command === 'copy') {
await copy(pack.linkFolderPath);
if (options.syncBuild) {
copy(pack.linkFolderPath, true);
} else {
await copy(pack.linkFolderPath);
}
}
} catch (error) {
spinner.stop();
Log.error(`\nAn error occured. While linking dependency. Error: ${error}`);
process.exit(1);
return;
}

spinner.stop();
Expand Down Expand Up @@ -140,8 +149,8 @@ export default async function(options, config) {
Log.info(`\nSymbolic link to ${packName} is successfully deleted.`);
} catch (error) {
spinner.stop();
Log.error(error);
process.exit(1);
Log.primary(`\n${packName} have not symbolic link`);
return;
}
}
});
Expand Down
24 changes: 19 additions & 5 deletions src/utils/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import fse from 'fs-extra';
import { Log } from './log';
import * as path from 'path';

export default async function(outputFolderPath) {
const packageJson = await fse.readJSON(`${outputFolderPath}/package.json`);
export default async function(outputFolderPath, sync) {
let packageJson;
if (sync) {
packageJson = fse.readJSONSync(`${outputFolderPath}/package.json`);
} else {
packageJson = await fse.readJSON(`${outputFolderPath}/package.json`);
}
const { name } = packageJson;
const hasScope = name.indexOf('@') > -1;

Expand All @@ -14,14 +19,23 @@ export default async function(outputFolderPath) {
}

if (fse.existsSync(path.resolve(`node_modules/${nodeModulePath}`))) {
await fse.remove(path.resolve(`node_modules/${nodeModulePath}`));
if (sync) {
fse.removeSync(path.resolve(`node_modules/${nodeModulePath}`));
} else {
await fse.remove(path.resolve(`node_modules/${nodeModulePath}`));
}
}

try {
await fse.copy(path.resolve(outputFolderPath), path.resolve(`node_modules/${nodeModulePath}`), { overwrite: true });
if (sync) {
fse.copySync(path.resolve(outputFolderPath), path.resolve(`node_modules/${nodeModulePath}`), { overwrite: true });
} else {
await fse.copy(path.resolve(outputFolderPath), path.resolve(`node_modules/${nodeModulePath}`), {
overwrite: true,
});
}
} catch (error) {
Log.error(`An error occured. While copying process. Error: ${error}`);
process.exit(1);
}
}

Expand Down

0 comments on commit 3a0f144

Please sign in to comment.