fs-extra#mkdirpSync TypeScript Examples

The following examples show how to use fs-extra#mkdirpSync. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UploadResourceUtil.ts    From joplin-utils with MIT License 5 votes vote down vote up
// Thanks to vs-picgo: https://github.com/Spades-S/vs-picgo/blob/master/src/extension.ts
  static async getClipboardImage(fileDir: string): Promise<IClipboardImage> {
    const baseDir = path.resolve(fileDir, 'ClipboardImage')
    mkdirpSync(baseDir)
    const imagePath = path.resolve(baseDir, `${Date.now()}.png`)
    return await new Promise<IClipboardImage>((resolve): void => {
      const platform: string = UploadResourceUtil.getCurrentPlatform()
      let execution
      // for PicGo GUI
      const platformPaths: {
        [index: string]: string
      } = {
        darwin: './clipboard/mac.applescript',
        win32: './clipboard/windows.ps1',
        win10: './clipboard/windows10.ps1',
        linux: './clipboard/linux.sh',
      }
      const scriptPath = path.join(__dirname, platformPaths[platform])
      if (platform === 'darwin') {
        execution = spawn('osascript', [scriptPath, imagePath])
      } else if (platform === 'win32' || platform === 'win10') {
        execution = spawn('powershell', [
          '-noprofile',
          '-noninteractive',
          '-nologo',
          '-sta',
          '-executionpolicy',
          'unrestricted',
          // fix windows 10 native cmd crash bug when "picgo upload"
          // https://github.com/PicGo/PicGo-Core/issues/32
          // '-windowstyle','hidden',
          // '-noexit',
          '-file',
          scriptPath,
          imagePath,
        ])
      } else {
        execution = spawn('sh', [scriptPath, imagePath])
      }

      execution.stdout.on('data', (data: Buffer) => {
        if (platform === 'linux') {
          if (data.toString().trim() === 'no xclip') {
            resolve({
              isExistFile: false,
              imgPath: 'xclip not found, Please install xclip before run picgo',
            })
            return
          }
        }
        const imgPath = data.toString().trim()
        let isExistFile = fs.existsSync(imgPath)
        // in macOS if your copy the file in system, it's basename will not equal to our default basename
        if (path.basename(imgPath) !== path.basename(imagePath)) {
          if (fs.existsSync(imgPath)) {
            isExistFile = true
          }
        }
        resolve({
          imgPath,
          isExistFile,
        })
      })
    })
  }
Example #2
Source File: wrapper.test.ts    From cli with MIT License 4 votes vote down vote up
describe('/test/wrapper.test.ts', () => {
  const wrapperPath = resolve(__dirname, './fixtures/wrapper');

  afterEach(async () => {
    await remove(wrapperPath);
  });

  describe('test all format', () => {
    beforeEach(async () => {
      await remove(wrapperPath);
      mkdirpSync(wrapperPath);
    });
    it('writeWrapper functionMap', async () => {
      const wrapperPath = resolve(__dirname, './fixtures/wrapper');
      writeWrapper({
        initializeName: 'initializeUserDefine',
        middleware: ['test1', 'test2'],
        cover: true,
        service: {
          functions: {
            aggregation: {
              handler: 'aggre.handler',
              _isAggregation: true,
              functions: ['index'],
              _handlers: [
                { path: '/api/test', handler: 'index.handler' },
                { path: '/*', handler: 'render.handler' },
              ],
            },
            index: {
              handler: 'index.handler',
              isFunctional: true,
              exportFunction: 'aggregation',
              sourceFilePath: 'fun-index.js',
            },
            render: {
              handler: 'render.handler',
            },
          },
        },
        baseDir: wrapperPath,
        distDir: wrapperPath,
        starter: 'testStarter',
        moreArgs: true,
      });
      const aggrePath = resolve(wrapperPath, 'aggre.js');
      const indexPath = resolve(wrapperPath, 'index.js');
      const renderPath = resolve(wrapperPath, 'render.js');

      assert(existsSync(aggrePath));
      assert(existsSync(indexPath));
      assert(existsSync(renderPath));
      const aggreContent = readFileSync(aggrePath).toString();
      assert(/exports\.initializeUserDefine\s*=/.test(aggreContent));
      assert(
        aggreContent.indexOf('handler.method.indexOf(currentMethod)') !== -1
      );
      assert(
        aggreContent.indexOf(
          'frameworkInstance.handleInvokeWrapper(handler.handler)(ctx, ...args)'
        ) !== -1
      );
      assert(aggreContent.indexOf('async (ctx, ...args)') !== -1);
      assert(
        /exports\.initializeUserDefine\s*=/.test(
          readFileSync(indexPath).toString()
        )
      );
      assert(
        /exports\.initializeUserDefine\s*=/.test(
          readFileSync(renderPath).toString()
        )
      );
    });

    it('writeWrapper aggregation', async () => {
      const wrapperPath = resolve(__dirname, './fixtures/wrapper');
      const registerFunction = resolve(wrapperPath, 'registerFunction.js');
      if (existsSync(registerFunction)) {
        await remove(registerFunction);
      }
      writeWrapper({
        initializeName: 'initializeUserDefine',
        middleware: ['test1', 'test2'],
        cover: true,
        service: {
          functions: {
            aggregation: {
              handler: 'aggre.handler',
              _isAggregation: true,
              functions: ['index'],
              _handlers: [
                {
                  path: '/api/test',
                  handler: 'index.handler',
                  argsPath: 'ctx.request.data.args',
                  isFunctional: true,
                  exportFunction: 'test',
                  sourceFilePath: 'fun-index.js',
                },
                { path: '/*', handler: 'render.handler' },
              ],
            },
          },
          layers: {
            testNpm: {
              path: 'npm:test',
            },
            'remote-debug': {
              path: 'oss:remote-debug',
            },
            testOss: {
              path: 'oss:test',
            },
          },
        },
        baseDir: wrapperPath,
        distDir: wrapperPath,
        starter: 'testStarter',
      });
      const aggrePath = resolve(wrapperPath, 'aggre.js');
      assert(
        /const layer_npm_testNpm = require\('test'\);/.test(
          readFileSync(aggrePath).toString()
        )
      );
      assert(/layer_oss_remote_debug/.test(readFileSync(aggrePath).toString()));
    });
    it('writeWrapper', async () => {
      const wrapperPath = resolve(__dirname, './fixtures/wrapper');
      const registerFunction = resolve(wrapperPath, 'registerFunction.js');
      if (existsSync(registerFunction)) {
        await remove(registerFunction);
      }
      writeWrapper({
        initializeName: 'initializeUserDefine',
        middleware: ['test1', 'test2'],
        cover: true,
        service: {
          functions: {
            aggregation: {
              handler: 'aggre.handler',
              _isAggregation: true,
              functions: ['index'],
              _handlers: [
                { path: '/api/test', handler: 'index.handler', method: 'get' },
                {
                  path: '/*',
                  handler: 'render.handler',
                  method: ['post', 'Get'],
                },
              ],
            },
            index: {
              handler: 'index.handler',
            },
            render: {
              handler: 'render.handler',
            },
          },
        },
        baseDir: wrapperPath,
        distDir: wrapperPath,
        starter: 'testStarter',
      });
      const aggrePath = resolve(wrapperPath, 'aggre.js');
      const indexPath = resolve(wrapperPath, 'index.js');
      const renderPath = resolve(wrapperPath, 'render.js');
      assert(existsSync(aggrePath));
      assert(existsSync(indexPath));
      assert(existsSync(renderPath));
      assert(!existsSync(registerFunction));
      assert(
        !/registerFunctionToIocByConfig/.test(
          readFileSync(aggrePath).toString()
        )
      );
      assert(
        readFileSync(aggrePath)
          .toString()
          .indexOf('handler.method.indexOf(currentMethod)') !== -1
      );
      assert(
        !/require\('registerFunction\.js'\)/.test(
          readFileSync(aggrePath).toString()
        )
      );
      assert(
        /exports\.initializeUserDefine\s*=/.test(
          readFileSync(aggrePath).toString()
        )
      );
      assert(
        /exports\.initializeUserDefine\s*=/.test(
          readFileSync(indexPath).toString()
        )
      );
      assert(
        /exports\.initializeUserDefine\s*=/.test(
          readFileSync(renderPath).toString()
        )
      );
    });
  });
  it('formetAggregationHandlers', async () => {
    const formatResult = formetAggregationHandlers([
      { path: '/api/1', eventType: 'http' },
      { path: '/api/', eventType: 'http' },
      { path: '/', eventType: 'http' },
      { path: '/api/*', eventType: 'http' },
      { path: '/api2', eventType: 'http' },
      { path: '/api/2', eventType: 'http' },
      { path: '/api', eventType: 'http' },
      { path: '/*', eventType: 'http' },
    ]);
    assert(formatResult[2].router === '/api/');
    assert(formatResult[3].router === '/api/**');
    assert(formatResult[4].router === '/api2');
    assert(formatResult[5].router === '/api');
    assert(formatResult[6].router === '/');
    assert(formatResult[7].router === '/**');
  });
});