Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

create-workspace

v0.0.11arrow_drop_down
v0.0.11
v0.0.10
v0.0.9
v0.0.8
v0.0.7
v0.0.6
v0.0.5
v0.0.4
v0.0.3
v0.0.2
v0.0.1
STATUS
Passing
DOWNLOADS
9,721
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
A month ago
SIZE
3 KB
2 contributors
Install create-workspace as a package?
Copied
npm i @bit/bit.envs.common.create-workspace
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
create-workspace.ts
74 Lines(68 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { promises as fs } from 'fs';
import mkdirp, { Options } from 'mkdirp';
import path from 'path';
import { getCapsuleName } from './get-capsule-name';

export async function createWorkspace(content: WorkspaceContent, options: WorkspaceOptions): Promise<string> {
  const targetDir = getCapsuleName('space');
  enrichContentWithDefaults(content, options);
  await createFS(targetDir, content);
  return targetDir;
}
export interface WorkspaceOptions {
  env: string;
  envTester?: string;
  name: string;
  packageJSON?: { [k: string]: any };
}

export interface WorkspaceContent {
  [k: string]: string;
}

function mkdirPromise(dir: string, opts: Options) {
  return new Promise((resolve, reject) => {
    mkdirp(dir, opts, (err, made) => (err === null ? resolve(made) : reject(err)));
  });
}

async function createFS(targetDir: string, content: WorkspaceContent) {
  await mkdirPromise(targetDir, {});
  await Promise.all(
    Object.keys(content).map(async key => {
      const realPath = path.join(targetDir, key);
      const containingFolder = path.dirname(realPath);
      await mkdirPromise(containingFolder, {});
      const filePath = path.resolve(targetDir, key);
      await fs.writeFile(filePath, content[key]);
    })
  );
}

function enrichContentWithDefaults(content: WorkspaceContent, options: WorkspaceOptions) {
  const packageJSON = Object.assign(
    {
      name: options.name,
      description: `Testing ${options.name}`,
      version: '0.0.1',
      bit: {
        env: {
          compiler: {
            meta: {
              options: {
                file: path.resolve(options.env)
              }
            }
          },
          tester: {
            meta2: {
              options: {
                file: options.envTester ? path.resolve(options.envTester) : ''
              }
            }
          }
        },
        componentsDefaultDirectory: 'components/{name}',
        packageManager: 'npm'
      }
    },
    options.packageJSON || {}
  );

  content['package.json'] = content['package.json'] || JSON.stringify(packageJSON, null, 4);
  content['.gitignore'] = content['.gitignore'] || `dist\nnode_modules\n`;
}